HttpWebRequest.BeginGetResponse 完成得太晚

发布于 2024-11-19 08:29:16 字数 2051 浏览 2 评论 0原文

我在代码中使用对 HttpWebRequest.BeginGetResponse() 方法的调用来从服务器获取数据。服务器生成的内容范围可能从几 KB 到几 GB。

我的问题是 HttpWebRequest.BeginGetResponse 完成得太晚了。它应该在建立与服务器的连接并收到 HTTP 标头后立即完成。

以下是使用 GET 方法的示例代码:

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”运行代码,结果看起来like:

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

可以在日志中轻松检测到问题。连接时间不可能超过一分钟,下载大约 72.5 MB 的时间不可能超过 38 毫秒。 似乎数据被下载到手机上的某个地方,并且仅当完整内容在本地可用时才会将 RequestComplete 通知发送到应用程序。这对我来说不好,因为我需要显示操作的进度。

我在 WP7 的设备和模拟器上得到了相同的结果(也在 WP7.1 上)。

我在 Windows 桌面上运行相同的代码并且运行正确:请求在一秒内完成,其余下载大约需要 1-2 分钟。

WP7或WP 7.1有什么解决方案吗? 新引入的 WP 7.1 API“后台文件传输”没有帮助,因为我需要完全控制 HTTP 标头和内容。并非我向服务器发出的所有 HTTP 请求都会生成文件作为输出。

谢谢!
米哈伊

I use in my code calls to HttpWebRequest.BeginGetResponse() method to get data from my server. The server produces content that may range from few KB to few GB.

My problem is that HttpWebRequest.BeginGetResponse completes too late. It should complete immediately after the connection to the server is established and the HTTP header is received.

Here is sample code using GET method:

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)
    {
    }
    .......
}

I run the code using "http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.39.1.tar.bz2" for example and the result looks like:

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

The problem can be easily detected in the log. It is not possible to spend more that one minute to connect and 38 ms to download about 72.5 MB.
It seems that the data is downloaded somewhere on the phone and the RequestComplete notification is sent to the application only when the full content is available locally. This is not OK for me because I need to show progress for the operation.

I get the same result on the device and emulator for WP7 (also on WP7.1).

I run same code on Windows desktop and it run correctly: the request completes within one second and the rest of the download takes about 1-2 minutes.

Is there any solution on WP7 or WP 7.1?
The newly introduced WP 7.1 API "Background File Transfers" does not help because I need full control over the HTTP headers and content. Not all HTTP requests that I make to the server produce files as output.

Thank you!
Mihai

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮色兮凉城 2024-11-26 08:29:16

如果您想向下传输数据,则需要禁用响应缓冲。您可以通过设置 AllowReadStreamBufferingfalse

HttpWebRequest myHttpWebRequest = WebRequest.CreateHttp(m_getUrl);
myHttpWebRequest.Method = "GET";
myHttpWebRequest.AllowReadStreamBuffering = false;

You need to disable response buffering if you want to stream the data down. You can do this by setting AllowReadStreamBuffering to false.

HttpWebRequest myHttpWebRequest = WebRequest.CreateHttp(m_getUrl);
myHttpWebRequest.Method = "GET";
myHttpWebRequest.AllowReadStreamBuffering = false;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文