【问题标题】:Convert simultaneous downloads with WebClient to HttpClient将使用 WebClient 的同时下载转换为 HttpClient
【发布时间】:2019-12-16 19:09:59
【问题描述】:

我有一个创建多个对象的 WinForms 应用程序,每个对象都有自己的 WebClient 并在其构造函数上调用 DownloadStringAsync。当回调 DownloadStringAsyncComplete 被调用时,显示被更新。 我想在这里保留的主要方面是能够同时执行多个请求,因为每个请求都需要几秒钟。 如何使用 HttpClient 完成此操作? 我无法等待 GetAsync,但是,我怎么知道每个响应何时到达? 如果我确实等待 GetAsync,那么它们将一个接一个地执行,整个过程将需要很长时间......

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Net;

namespace RequestTest
{
    public partial class Form1 : Form
    {
        List<Requester> requesters;

        public Form1()
        {
            InitializeComponent();

            requesters = new List<Requester>();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 20; i++)
            {
                requesters.Add(new Requester(this));
            }
        }

        public void SomeoneCompletedDownload()
        {
            textBox1.AppendText("sample text");
        }
    }

    public class Requester
    {
        Form1 caller;

        public Requester(Form1 _caller)
        {
            caller = _caller;

            var client = new WebClient();
            client.DownloadStringCompleted += DownloadCompleted;
            client.DownloadStringAsync(new Uri("some-url"));
        }

        private void DownloadCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            caller.SomeoneCompletedDownload();
        }
    }
}

【问题讨论】:

  • 这能回答你的问题吗? Download multiple files in parallel using c#
  • 一架飞机从伦敦飞到纽约需要8小时...如果你有8架飞机需要多少? ...似乎您相信这需要 1 小时 ...
  • @Selvin 确实需要更少的时间,因为大部分时间都花在了服务器端。
  • @Eldar 不是真的。我的应用程序当前可以工作,但根据 Microsoft 的说法,不应使用 WebClient 类,而应使用 HttpClient 。我正在尝试在我的应用中查看如何操作。

标签: c# httpclient webclient


【解决方案1】:

好的,我找到了自己的答案。 我只是将请求移到了一个可等待的方法。

方法本身等待响应并调用回调。

但在调用该方法时,我并不等待它完成。

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Http;

namespace RequestTest
{
    public partial class Form1 : Form
    {
        List<Requester> requesters;

        DateTime start;
        int counter;

        public Form1()
        {
            InitializeComponent();

            requesters = new List<Requester>();
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            start = DateTime.Now;

            for (int i = 0; i < 30; i++)
            {
                requesters.Add(new Requester(this, i + 1));

                // only one of the following 3 lines should be uncommented
                //requesters[i].RequestWeb(); // this uses the old WebClient (~ 12 sec)
                //await requesters[i].RequestHttp(); // this takes forever since requests are executed one at a time (~ 86 sec)
                _ = requesters[i].RequestHttp(); // <-- This is the way to go for my app. Dispatch the request and continue executing discarding the result. Callback is called in the same method. (~ 13 sec, consistent with option #1)
            }
        }

        public void SomeoneCompletedDownload(string page)
        {
            var elapsed = DateTime.Now - start;
            counter++;
            textBox1.AppendText($"#{counter}: Page {page} finished after {elapsed.TotalSeconds.ToString("N2")} seconds.\r\n");
        }
    }

    public class Requester
    {
        Form1 caller;
        string page;
        string url;

        public Requester(Form1 _caller, int _page)
        {
            caller = _caller;
            page = _page.ToString();

            url = "https://www.youtube.com/";
        }

        public void RequestWeb()
        {
            var client = new WebClient();
            client.DownloadStringCompleted += DownloadCompleted;
            client.DownloadStringAsync(new Uri(url));
        }

        public async Task RequestHttp()
        {
            var client = new HttpClient();
            var response = await client.GetAsync(url);
            caller.SomeoneCompletedDownload(page);
        }

        private void DownloadCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            caller.SomeoneCompletedDownload(page);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多