c#WebRequest不会发布整个数据长度

时间:2012-11-11 19:54:44

标签: c# php post httpwebrequest webrequest

我目前遇到了问题。 我试图将我的数据发布到PHP文档,但它没有得到全部价值。 在中间某处停止发布。

有谁知道问题所在? bytearray长7401。那不能长篇大论吗?

我的代码如下:

public string RecieveData(string url, string postData = "")
    {

            WebRequest request = WebRequest.Create(url);
            // If required by the server, set the credentials.

            NetworkCredential nc = new NetworkCredential("user", "pass");
            Stream dataStream;  

            if (postData != "")
            {
                // Set the Method property of the request to POST.
                request.Method = "POST";
                // Create POST data and convert it to a byte array.string postData = "This is a test that posts this string to a Web server.";
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                // Set the ContentType property of the WebRequest.
                request.ContentType = "application/x-www-form-urlencoded";
                // Set the ContentLength property of the WebRequest.
                request.ContentLength = byteArray.Length;
                // Get the request stream.
                dataStream = request.GetRequestStream();
                // Write the data to the request stream.
                dataStream.Write(byteArray, 0, byteArray.Length);
                // Close the Stream object.
                dataStream.Close();
            }

            request.Credentials = nc;
            // Get the response.
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            // Display the status.
            Console.WriteLine(response.StatusDescription);
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content. 
            string responseFromServer = reader.ReadToEnd();
            // Display the content.
            Console.WriteLine(responseFromServer);
            // Cleanup the streams and the response.
            reader.Close();
            dataStream.Close();
            response.Close();

            return responseFromServer;
        /*
        }
        catch (Exception)
        {
            MessageBox.Show("Er is iets fout gegaan met verbinden");
            return "";
        }
        */

    }

1 个答案:

答案 0 :(得分:0)

7401不会太长。

我的猜测是您发布的数据未经过完全网址编码,例如:字节数组中的一个字符会导致PhP解析器停止。确保您正在查看原始数据(例如使用Wireshark)。