2

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

Mihai
  • 89
  • 6

1 Answers1

3

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;
Richard Szalay
  • 83,269
  • 19
  • 178
  • 237
  • I have one more question. This is related to stream read cancel. I posted it on 'http://stackoverflow.com/questions/6679480/how-to-cancel-reading-from-a-stream-obtained-using-httpwebresponse-getresponsestr' Thanks! – Mihai Jul 13 '11 at 13:57