[英]HttpWebRequest Post Method sending partial Data
I have the following code to send a POST request to Server. 我有以下代码向服务器发送POST请求。
HttpWebRequest myWebRequest = (HttpWebRequest)HttpWebRequest.Create(uri);
myWebRequest.Method = WebRequestMethods.Http.Post;
myWebRequest.ContentLength = data.Length;
myWebRequest.ContentType = "application/x-www-form-urlencoded";
StreamWriter writer = new StreamWriter(myWebRequest.GetRequestStream());
writer.Write(data);
writer.Close();
The data to be posted is an XML request with nearly 2000 Characters. 要发布的数据是一个包含近2000个字符的XML请求。 I read the response as shown below. 我阅读了如下所示的回复。
this.Response.ContentType = "text/xml";
StreamReader reader = new StreamReader(this.Request.InputStream);
string responseData = reader.ReadToEnd();
The response data that we are obtaining above has only a portion of the actual data send. 我们上面获得的响应数据只发送了一部分实际数据。
Please help me to get the full data. 请帮我获取完整的数据。
This worked for me, i think you miss the Encoding... using this i'm sending huge xml to server 这对我有用,我想你错过了编码...使用这个我正在向服务器发送巨大的xml
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(connectionString);
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
// 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();
WebResponse response = request.GetResponse();
//Console.WriteLine("Service Status Code:"+((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.
string responseFromServer = reader.ReadToEnd();
The way I've been doing it is only slightly different than your way: 我一直这样做的方式与你的方式略有不同:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL);
request.Method = WebRequestMethods.Http.Post;
request.ContentLength = DataToPost.Length;
request.ContentType = "application/x-www-form-urlencoded";
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(DataToPost);
writer.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
This way has been working for years. 这种方式已经工作多年了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.