I use a HttpWebRequest in C# for Windows Phone 8 to download a file from a remote server. The request is canceled after a certain time using a timeout which calls HttpWebRequest.Abort().

This works fine, but I would like to get access to the data which has already been downloaded, including the headers sent by the server, even if the download (request) has not been completed yet:

HttpWebRequest  _request =  (HttpWebRequest)WebRequest.Create("http://urltofile.zip");
_request.Method = "GET";

_timeout.Start(); // a timer which calls _request.Abort() after a certain time
request.BeginGetResponse(new AsyncCallback(HandleResultAsync), _request);



public void HandleResultAsync(IAsyncResult result)
{
    HttpWebRequest request = (HttpWebRequest)result.AsyncState;

    HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result); // exception is raised here when "_request.Abort()" is called from the timer
}

When "HttpWebRequest.Abort()" is called from the timer, then the method "HandleResultAsync" is invoked. But accessing "request.EndGetResponse()" to get the already downloaded header and partial content fails because the response has already ended.

How can I get access to the eventually downloaded header and partial content?

Regards,

有帮助吗?

解决方案

I found a solution: you have to turn off buffering for the HttpWebRequest, and then it is possible to just cancel the download while reading chunks of data within the "HandleResultAsync" method from my initial post:

_request.AllowReadStreamBuffering = false;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top