3

我正在使用以下代码执行 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();
4

1 回答 1

2

Uri.EscapeDataString(json) 成功了。

于 2012-07-27T19:46:54.457 回答