Windows phone 7 HttpWebRequest.BeginGetResponse完成得太晚
我在代码调用中使用HttpWebRequest.BeginGetResponse()方法从服务器获取数据。服务器生成的内容可能从几KB到几GB不等 我的问题是HttpWebRequest.BeginGetResponse完成得太晚。它应该在与服务器建立连接并收到HTTP头后立即完成 下面是使用GET方法的示例代码:Windows phone 7 HttpWebRequest.BeginGetResponse完成得太晚,windows-phone-7,httpwebrequest,progress,Windows Phone 7,Httpwebrequest,Progress,我在代码调用中使用HttpWebRequest.BeginGetResponse()方法从服务器获取数据。服务器生成的内容可能从几KB到几GB不等 我的问题是HttpWebRequest.BeginGetResponse完成得太晚。它应该在与服务器建立连接并收到HTTP头后立即完成 下面是使用GET方法的示例代码: public bool StartDownload() { try { HttpWebRequest myHttpWebRequest = (Http
public bool StartDownload()
{
try
{
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(m_getUrl);
myHttpWebRequest.Method = "GET";
// Start the asynchronous request.
m_requestState = new RequestState();
m_requestState.request = myHttpWebRequest;
myHttpWebRequest.BeginGetResponse(new AsyncCallback(ResponseCompleted), m_requestState);
}
catch (Exception)
{
m_requestState = null;
}
return m_requestState != null;
}
private void ResponseCompleted(IAsyncResult result)
{
RequestState myRequestState = (RequestState)result.AsyncState;
HttpWebRequest myHttpWebRequest = myRequestState.request;
m_logger.LogMessage("ResponseCompleted notification received!");
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)myHttpWebRequest.EndGetResponse(result);
}
catch (Exception)
{
}
.......
}
我使用“http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.39.1.tar.bz2“例如,结果如下所示:
hh:mm:ss.ms
12:51:30.9440000 - Download started!
12:53:04.8520000 - ResponseCompleted notification received!
12:53:04.8560000 - Header received!
12:53:04.8570000 - DataReceived: 524288 bytes
.........................................
12:53:04.8940000 - DataReceived: 78818 bytes
12:53:04.8940000 - Request data received!
12:53:04.8940000 - Received bytes: 76100578
在日志中可以很容易地检测到问题。连接时间不可能超过一分钟,下载时间不可能超过38毫秒,大约72.5 MB。
似乎数据是在手机上的某个地方下载的,只有当完整内容在本地可用时,才会向应用程序发送RequestComplete通知。这对我来说不好,因为我需要显示操作的进度
我在WP7的设备和模拟器上得到了相同的结果(同样在WP7.1上)
我在Windows桌面上运行了相同的代码,并且运行正常:请求在一秒钟内完成,其余的下载大约需要1-2分钟
WP7或WP 7.1是否有任何解决方案?
新引入的WP7.1API“后台文件传输”没有帮助,因为我需要完全控制HTTP头和内容。并非我对服务器发出的所有HTTP请求都会生成文件作为输出
谢谢大家!Mihai如果要流式传输数据,需要禁用响应缓冲。您可以通过设置为
false
来执行此操作
HttpWebRequest myHttpWebRequest = WebRequest.CreateHttp(m_getUrl);
myHttpWebRequest.Method = "GET";
myHttpWebRequest.AllowReadStreamBuffering = false;
我还有一个问题。这与流读取取消有关。我把它贴在了“谢谢!