[英]Why HttpWebRequest and method POST data is not seen by the server
[英]HttpWebRequest Post Method sending partial Data
我有以下代碼向服務器發送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();
要發布的數據是一個包含近2000個字符的XML請求。 我閱讀了如下所示的回復。
this.Response.ContentType = "text/xml";
StreamReader reader = new StreamReader(this.Request.InputStream);
string responseData = reader.ReadToEnd();
我們上面獲得的響應數據只發送了一部分實際數據。
請幫我獲取完整的數據。
這對我有用,我想你錯過了編碼...使用這個我正在向服務器發送巨大的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();
我一直這樣做的方式與你的方式略有不同:
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());
這種方式已經工作多年了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.