WP7(Windows Phone 7)HttpWebRequest 丢失 POST 数据
我正在发送大量 POST 数据(超过 5000 个字符),并且 WP7 HttpWebRequest 似乎正在丢失该过程中的一些内容...
数据是转换为字节数组的 png,然后转换为 base64 字符串。
如果我的数据长度是 4941 个字符,它只会发送 1448 个字符,请求的内容长度(使用wireshark验证)没问题(内容长度:4991)。
我如何发送数据:
void SendPost()
{
// Create the web request object
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
// Start the request
webRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), webRequest);
}
void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
// End the stream request operation
Stream postStream = webRequest.EndGetRequestStream(asynchronousResult);
// Create the post data
// Demo POST data (length = 3762)
string postData = "data=iVBORw0KGgoAAAANSUhEUgAAAFoAAAASCAYAAADbo8kDAAAKyElEQVR4nG2YB3TNVxzHm5eEDHuF2GJUjVRqb2okiMRWqtG0KKI4aq8mUQQ5FRWrhNi1Z6lWazSrRm1iVBDESiIxEkH6Pu/069zzTt857/zv//5/e9/73tu3bx3s/2/evLHk5eU5vX792pG19vT91atXzlo/f/7cTWtweL58+dLFpGWPb/+HT25ubgHzXbTs8SSbKQvv7IOnPf56t8fn+eLFC1d4Sn6TD+ucnJyCpjw87WU06UoO7UtOyWZ5779ffn6+g5U4BrI4ODjkOzo6Wr9a3rLmO/tWYjCysC88Z2fnPPZZWwk68ixYsGBudnZ2YdbgWw3vKjpWxs5WgVHCIrqio7VVWCf428smeiZ/Jyen18BqHxmAZ593aCEf+/x5B8/FxSUHnq6uri/FE9mQAXyTh2hqj3dzLRllM8nJ+zvZBMQf5gKEMQYRc/YLFCjwinfWPIGRouDwXQYuVKjQMwmq7xIGR4gPe3KC9qApJZCB7+DIGLzLcKJh8sKAwPEdWrxLYdbQFK7oACdnmfRwEjgynCmn8L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/Xq1fvPGMYYxL1nSaLEkwM8Bg2bNjy+vXrn0MpX1/fg9RVFMbpiYmJTeEJPxoV0UXalypV6jFRCe8aNWpcCwkJWXzp0qUPUIjMgCbTAgYmKGigyEspAQbYoUOHrgAGGci8fv36/YRj6Esqo9R5jEo5Qi5sRJQiL/jnz5+vh3zUYnQlqsHVWQEbYDeVW9vcS7Qxu3JQYPrQiYzRjPBfuHDhGIAxJu8ozAQBYQyBEVRLzbsEjDVhwoR5OANvozxdnKgmRVGA6J00adJc0hX6TAKCIVWJBuosHZw6SinB4fw5QGBYaIBL5mA0HAoePJkAaHw0LfRi0qEHYWDJS9PDWJQOyU99pbQQGMpi6NWqVSt5+PDhSwlMnAMMTZeJA5lwCDKSiYzGyGUbgSHKR2bIrVu39qEU8AHjM7NyKEAo9oDFEKQOMy8wpA0GY01k4jjdjeBddWDqHDjULzJFYyKnKOobzRd8DgekKydUeAEDTcoThmRG1R3IxYsX66AM8yo8iSDKCyUDR2IIdOAkSACBhxOZjamlykAOODR+5OUdYyMf9OhXwOrAdfny5do4npJB9pEFGzduHEDmQp+SRiagEw5CfluNlgdRijWRoPO6WXNhrEsdRQKMdewmgvEeBtdhRkbSdAANnmoiomPO44pE1rrpEx3zEkl4Jl3hmpdOasI6AYuvDKdMlczQMEdW0UYWjba6+WOtSJdc0kE8Vf8tutDRxYpqsXnjBhNdIomobtNYUxI0gZAdarD2+GIuZ4mXaQw5lie8ZGAMY8pnTjpyCkpJF53I9E3NXFe9GMN+bJPh1dhN+uDrgkvy6Bt2YU83hnKAHGijJ4/ooKDDhBTWAcGcYcUETymNEVqe5LsMDp4Gdwll3sDpEGJeVbJnRrkizbxatTeGIg85dKGliNI1KU/JaO8gXcVqXyVEcsiYkkcThmZ98DViSm+VJt7/BbjvID507t6TAAAAAElFTkSuQmCC";
//Actual code used
/*for (int i = 0; i < paramNames.Count; i++)
{
// Parameter seperator
if (i > 0)
{
postData += "&";
}
// Parameter data
postData += paramNames[i] + "=" + paramValues[i];
}*/
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Add the post data to the web request
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
// Start the web request
webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
}
void GetResponseCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response;
// End the get response operation
response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamReader = new StreamReader(streamResponse);
Response = streamReader.ReadToEnd();
streamResponse.Close();
streamReader.Close();
response.Close();
}
catch (WebException e)
{
// Error treatment
// ...
}
}
我怎样才能完整地发送我的数据?
谢谢 !
I'm sending a lot of POST data (over 5000 chars) and it seems that WP7 HttpWebRequest is losing some of the content in the process...
The data is a png converted to a byte array and after to a base64 string.
If my data is 4941 characters long, it will send only 1448 chars, the content length (verified with wireshark) of the request is fine (Content-Length: 4991).
How i send the data :
void SendPost()
{
// Create the web request object
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
// Start the request
webRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), webRequest);
}
void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
// End the stream request operation
Stream postStream = webRequest.EndGetRequestStream(asynchronousResult);
// Create the post data
// Demo POST data (length = 3762)
string postData = "data=iVBORw0KGgoAAAANSUhEUgAAAFoAAAASCAYAAADbo8kDAAAKyElEQVR4nG2YB3TNVxzHm5eEDHuF2GJUjVRqb2okiMRWqtG0KKI4aq8mUQQ5FRWrhNi1Z6lWazSrRm1iVBDESiIxEkH6Pu/069zzTt857/zv//5/e9/73tu3bx3s/2/evLHk5eU5vX792pG19vT91atXzlo/f/7cTWtweL58+dLFpGWPb/+HT25ubgHzXbTs8SSbKQvv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jBhNdIomobtNYUxI0gZAdarD2+GIuZ4mXaQw5lie8ZGAMY8pnTjpyCkpJF53I9E3NXFe9GMN+bJPh1dhN+uDrgkvy6Bt2YU83hnKAHGijJ4/ooKDDhBTWAcGcYcUETymNEVqe5LsMDp4Gdwll3sDpEGJeVbJnRrkizbxatTeGIg85dKGliNI1KU/JaO8gXcVqXyVEcsiYkkcThmZ98DViSm+VJt7/BbjvID507t6TAAAAAElFTkSuQmCC";
//Actual code used
/*for (int i = 0; i < paramNames.Count; i++)
{
// Parameter seperator
if (i > 0)
{
postData += "&";
}
// Parameter data
postData += paramNames[i] + "=" + paramValues[i];
}*/
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Add the post data to the web request
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
// Start the web request
webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
}
void GetResponseCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response;
// End the get response operation
response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamReader = new StreamReader(streamResponse);
Response = streamReader.ReadToEnd();
streamResponse.Close();
streamReader.Close();
response.Close();
}
catch (WebException e)
{
// Error treatment
// ...
}
}
How can I send my data entirely ?
Thank you !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 RunSat 中,我发送的字节数比使用下面的代码多得多 - 它与您显示的代码类似,并且工作正常(尽管我承认我主要在真实设备上进行测试),
具体取决于您对 WireShark 的专业程度,它可能并不是告诉你全部真相 - 当你查看wireshark时,你会看到 TCP 级别的数据包(对于 ip4)通常有 1448 字节的有效负载 - http://en.wikipedia.org/wiki/TCP_segmentation_offload - 我发现很容易错过延续数据包。 (我不是wireshark用户,但使用过一点ethereal)
更新:我在下面运行了这段代码
它工作正常 - 你可以在http://posttestserver.com/data/2011/05/12/03.29.331923823079
我建议您停止使用 WireShark 在以太网和 TCP/IP 数据包级别上搞乱,看一下在您的网络服务上。
In RunSat I send plenty more bytes than that using the code below - its similar to the code you've shown and it works fine (although I admit I mainly test on a real device)
Depending on how expert you are with WireShark, it might not be telling you the whole truth - when you're looking at wireshark you will see the TCP level packets which (for ip4) commonly have 1448 bytes of payload - http://en.wikipedia.org/wiki/TCP_segmentation_offload - and I find it easy to miss the continuation packets. (I'm not a wireshark user, but have used ethereal a bit)
Update: I've run this code below
It works fine - you can see the upload at http://posttestserver.com/data/2011/05/12/03.29.331923823079
I recommend you stop messing about at the ethernet and TCP/IP packet levels with WireShark and take a look at your webservice.