我在网上搜索,但在使用 HttpWebRequest 下载文件时找不到任何获得进度的方法。这门课是否支持进步?任何链接、教程、提示将不胜感激。
谢谢。
PS这是代码...
private static Task<HttpResponse> MakeAsyncRequest(string requestString)
{
var request = (HttpWebRequest)WebRequest.Create(requestString);
Task<WebResponse> requestTask = Task.Factory.FromAsync(
request.BeginGetResponse,
asyncResult => request.EndGetResponse(asyncResult),
null);
return requestTask.ContinueWith(t => ReadStreamFromResponce(t.Result));
}
private static HttpResponse ReadStreamFromResponce(WebResponse result)
{
var responseobject = new HttpResponse();
var response = (HttpWebResponse)result;
responseobject.StatusCode = (short)response.StatusCode;
if (!IsSuccess(responseobject.StatusCode))
return responseobject;
using (var responseStream = response.GetResponseStream())
using (var ms = new MemoryStream())
{
responseStream.CopyTo(ms);
responseobject.SetResponse(ms.ToArray());
return responseobject;
}
}