【问题标题】:Webclient + UploadStringAsync + Events: UploadProgressChanged / DownloadProgressChangedWebclient + UploadStringAsync + 事件:UploadProgressChanged / DownloadProgressChanged
【发布时间】:2019-04-05 18:12:44
【问题描述】:

我需要与 REST 服务通信。

由于某些原因,我必须使用 POST 请求从该服务获取 JSON 数据。 (我认为它应该使用 GET 请求,但这不在我的控制范围内,我无法更改它......)

一个请求的响应大约为 25-30 MB。 由于响应很大,我决定使用 WebClient 和 UploadStringAsync 方法。

private string jsonResult = "";

void test()
{
    string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + passowrd));  
    using (System.Net.WebClient client = new System.Net.WebClient())
    {
        client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
        client.UploadProgressChanged += new UploadProgressChangedEventHandler(client_UploadProgressHandler);
        client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompletedHandler);                
        client.Headers.Add("Authorization", "Basic " + credentials);
        client.Encoding = System.Text.Encoding.UTF8;
        Uri uri = new Uri(https://demoapi.org/stuff/prod/0);

        client.UploadStringAsync(uri, "POST", "");
    }
}

private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
    Console.WriteLine("DOWNLOAD_PROGRESS!");
    Console.WriteLine("Status {0} of {1} bytes. {2} % complete", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage);
}

private static void client_UploadProgressHandler(object sender, UploadProgressChangedEventArgs e)
{
    Console.WriteLine("UPLOAD_PROGRESS!");
    Console.WriteLine("Status {0} of {1} bytes. {2} % complete, {3} of {4} bytes. {5} % complete", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage, e.BytesSent, e.TotalBytesToSend, e.ProgressPercentage);
}

private void client_UploadStringCompletedHandler(Object sender, UploadStringCompletedEventArgs e)
{
    Console.WriteLine("UPLOAD_COMPLETED!");
    jsonResult = e.Result;
}

使用上面的代码,控制台多次显示消息UPLOAD_PROGRESS!并且一旦消息 UPLOAD_COMPLETED!但是,我没有看到消息 DOWNLOAD_PROGRESS!

UPLOAD_PROGRESS!
UPLOAD_PROGRESS!
....    
UPLOAD_PROGRESS!
UPLOAD_COMPLETED!

这是我的问题:

1) 我可以(真的)使用 webclient.UploadStringAsync 方法订阅“UploadProgressChanged”事件吗?

Microsoft 文档没有说 UploadProgressChanged 可用于 UploadStringAsync,但无论如何它似乎与上面的代码一起工作。

https://docs.microsoft.com/en-us/dotnet/api/system.net.webclient.uploadprogresschanged?view=netframework-4.7.2

https://docs.microsoft.com/en-us/dotnet/api/system.net.webclient.uploadstringasync?view=netframework-4.7.2

我还查看了 Microsoft 参考源,但我并不完全理解它。 UploadStringAsync 方法中似乎不存在委托“UploadProgressChanged”。

https://referencesource.microsoft.com/#System/net/System/Net/webclient.cs,d15a560fedc713da,references

所以,我想知道将“UploadProgressChanged”与 webclient.UploadStringAsync 一起使用是否正确。

2) 我可以使用 webclient.UploadStringAsync 方法订阅“DownloadProgressChanged”事件吗?

3) 据我了解,网络客户端在后台使用 WebRequest 和 WebResponse。是否可以获得两个请求的进度?

我问这个是因为有时请求的主体很大,有时响应很大。

4) 如果 DownloadProgressChangedEvent 不可用,则 如何报告“UploadStringAsync”POST 请求的 WebResponse 部分的进度?

谢谢

【问题讨论】:

  • 您上面的代码进行了上传,因此它无法接收到 DownloadProgressChanged 事件。规则是所有一个 Download 或 Upload 异步方法引发相应的 Download 或 Upload 进度更改事件。非异步方法不会引发进度事件。如果你想访问底层的 WebRequest,只需创建一个派生自 WebClient 的类并覆盖受保护的虚拟 GetWebRequest(Uri address) 方法。
  • 好的,我知道 Upload 方法只会引发 Upload 事件。但是,文档没有说明 UploadStringAsync 的 ProgressChanged。我可以确定为 UploadStringAsync 调用 Progress Change 吗?我也没有在参考源代码中看到委托/回调。它似乎正在使用上面的代码,但我不明白为什么文档和代码没有提及或使用它。PS:你能在下面发布答案,我会给你赏金。

标签: c# .net rest httpwebrequest webclient


【解决方案1】:

您上面的代码进行了上传,因此它无法接收DownloadProgressChanged 事件。

规则是所有一个下载或上传异步方法引发相应的下载或上传进度更改事件。在场景下,UploadStringAsync 使用与 UploadDataAsync 相同的代码(因为字符串是一个字节数组,以字符编码为模)。

非异步方法不会引发进度事件。

如果您想访问底层 WebRequest,只需创建一个派生自 WebClient 的类并覆盖 protected virtual GetWebRequest(Uri address) 方法,如下所示:

public class MyWebClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        var request = base.GetWebRequest(address);
        // do something with the request
        // BTW, this is how you can change timeouts or use cookies
        return request;
    }
}

请注意,您还可以使用更现代的 HttpClient 类(完全跨平台、框架等)代替 WebClient。

【讨论】:

    猜你喜欢
    • 2023-01-18
    • 1970-01-01
    • 2011-06-26
    • 2016-10-14
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    相关资源
    最近更新 更多