gpt4 book ai didi

c# - WP7 (windows phone 7) HttpWebRequest 丢失 POST 数据

转载 作者:可可西里 更新时间:2023-11-01 08:45:16 26 4
gpt4 key购买 nike

我正在发送大量 POST 数据(超过 5000 个字符),并且 WP7 HttpWebRequest 似乎在这个过程中丢失了一些内容......

数据是 png 转换为字节数组,然后转换为 base64 字符串。

如果我的数据是 4941 个字符长,它只会发送 1448 个字符,请求的内容长度(使用 wireshark 验证)没问题(Content-Length: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/YLFCjwinfWPIGRouDwXQYuVKjQMwmq7xIGR4g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{
// 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
// ...
}
}

我怎样才能完整地发送我的数据?

谢谢!

最佳答案

在 RunSat 中,我发送的字节数比使用下面的代码发送的字节数多得多 - 它类似于您显示的代码并且工作正常(尽管我承认我主要在真实设备上进行测试)

            Stream requestStream = CurrentWebRequest.EndGetRequestStream(async);
byte[] encodedBytes = Encoding.UTF8.GetBytes(payload);
requestStream.Write(encodedBytes, 0, encodedBytes.Length);
requestStream.Flush();
requestStream.Close();

根据您对 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 数据包级别搞乱并查看您的网络服务。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace DemoProject
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
SendPost();
}

void SendPost()
{
var url = "http://posttestserver.com/post.php";

// 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
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+DIGLzLcKJh8sKAwPEdWrxLYdbQFK7oACdnmfRwEjgynCmn8LEPMKJpfkNmm5NkZEUN0QcijGUQGYPv7Ek5YICVx/kXLlw4W44SQ5OG6UgZnn2UNx0so+F81tYUd5fRRFcZYP6sJcHNjHQZy5RJf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jMgCbTAgYmKGigyEspAQbYoUOHrgAGGci8fv36/YRj6Esqo9R5jEo5Qi5sRJQiL/jnz5+vh3zUYnQlqsHVWQEbYDeVW9vcS7Qxu3JQYPrQiYzRjPBfuHDhGIAxJu8ozAQBYQyBEVRLzbsEjDVhwoR5OANvozxdnKgmRVGA6J00adJc0hX6TAKCIVWJBuosHZw6SinB4fw5QGBYaIBL5mA0HAoePJkAaHw0LfRi0qEHYWDJS9PDWJQOyU99pbQQGMpi6NWqVSt5+PDhSwlMnAMMTZeJA5lwCDKSiYzGyGUbgSHKR2bIrVu39qEU8AHjM7NyKEAo9oDFEKQOMy8wpA0GY01k4jjdjeBddWDqHDjULzJFYyKnKOobzRd8DgekKydUeAEDTcoThmRG1R3IxYsX66AM8yo8iSDKCyUDR2IIdOAkSACBhxOZjamlykAOODR+5OUdYyMf9OhXwOrAdfny5do4npJB9pEFGzduHEDmQp+SRiagEw5CfluNlgdRijWRoPO6WXNhrEsdRQKMdewmgvEeBtdhRkbSdAANnmoiomPO44pE1rrpEx3zEkl4Jl3hmpdOasI6AYuvDKdMlczQMEdW0UYWjba6+WOtSJdc0kE8Vf8tutDRxYpqsXnjBhNdIomobtNYUxI0gZAdarD2+GIuZ4mXaQw5lie8ZGAMY8pnTjpyCkpJF53I9E3NXFe9GMN+bJPh1dhN+uDrgkvy6Bt2YU83hnKAHGijJ4/ooKDDhBTWAcGcYcUETymNEVqe5LsMDp4Gdwll3sDpEGJeVbJnRrkizbxatTeGIg85dKGliNI1KU/JaO8gXcVqXyVEcsiYkkcThmZ98DViSm+VJt7/BbjvID507t6TAAAAAElFTkSuQmCCTHEEND";

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);
var Response = streamReader.ReadToEnd();
streamResponse.Close();
streamReader.Close();
response.Close();

}
catch (WebException e)
{
// Error treatment
// ...
}
}
}
}

关于c# - WP7 (windows phone 7) HttpWebRequest 丢失 POST 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5952094/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com