HTTP Post切断字符串

时间:2012-04-24 23:03:36

标签: c# http post

我使用以下代码来执行HTTP Post。这在大多数情况下都可以正常工作,但是在一定长度上切断我的字符串大约4300个字符串字符。我怎样才能解决这个问题?我有一种预感,这与所有未在帖子中发布并被截断的数据有关。我怎样才能解决这个问题呢?

ASCIIEncoding encoding = new ASCIIEncoding();
                // Create a request using a URL that can receive a post. 
                WebRequest request = WebRequest.Create("<URL>");
                // Set the Method property of the request to POST.
                request.Method = "POST";
                // Create POST data and convert it to a byte array.
                string postData = "json=" + json;
                //string postData = "json=blah";
                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.
                Stream dataStream = request.GetRequestStream();
                // Write the data to the request stream.
                dataStream.Write(byteArray, 0, byteArray.Length);
                // Close the Stream object.
                dataStream.Close();
                // Get the response.
                WebResponse response = request.GetResponse();
                // Display the status.
                //Console.WriteLine(((HttpWebResponse)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.
                retVal = reader.ReadToEnd();

                // Clean up the streams.
                reader.Close();
                dataStream.Close();
                response.Close();

1 个答案:

答案 0 :(得分:2)

那个Uri.EscapeDataString(json)完成了这个伎俩。