【问题标题】:Converting WebClient code to HttpClient code将 WebClient 代码转换为 HttpClient 代码
【发布时间】:2021-01-15 21:00:34
【问题描述】:

如果可能的话,我需要帮助将此 WebClient 转换为 HttpClient 代码,如果可以的话,我会非常高兴。

我一直在寻找可以将其转换为我的东西/人,但遗憾的是我没有找到任何东西。

谢谢!

    private void bunifuFlatButton9_Click(object sender, EventArgs e)
    {
        {
            using (WebClient webClient = new WebClient())
            {
                webClient.Encoding = Encoding.UTF8;
                webClient.DownloadDataCompleted += new DownloadDataCompletedEventHandler(this.Complete);
                webClient.DownloadDataAsync(new Uri("https://www.bing.com/search?q=site:pastebin.com+" + this.textBox1.Text));
                webClient.DownloadDataAsync(new Uri("https://www.bing.com/search?q=site:pastebin.com+" + this.textBox1.Text));
                webClient.DownloadDataAsync(new Uri("https://duckduckgo.com/?q=site%3Apastebin.com+" + this.textBox1.Text));
                webClient.DownloadDataAsync(new Uri("https://duckduckgo.com/?q=site%3Apastebin.com+" + this.textBox1.Text));
                webClient.DownloadDataAsync(new Uri("https://duckduckgo.com/?q=site%3Apastebin.com+" + this.textBox1.Text));
                webClient.DownloadDataAsync(new Uri("https://yandex.ru/search/?text=" + this.textBox1.Text));
                webClient.DownloadDataAsync(new Uri("https://yandex.ru/search/?text=" + this.textBox1.Text));
                webClient.DownloadDataAsync(new Uri("https://yandex.ru/search/?text=" + this.textBox1.Text));
                webClient.DownloadDataAsync(new Uri("https://yandex.ru/search/?text=" + this.textBox1.Text));
                webClient.DownloadDataAsync(new Uri("https://yandex.ru/search/?text=" + this.textBox1.Text));

            }
        }
    }

    private void Complete(object sender, DownloadDataCompletedEventArgs e)
    {
        MatchCollection matchCollection = Regex.Matches(new UTF8Encoding().GetString(e.Result), "(https:\\/\\/pastebin.com\\/\\w+)");
        int num = checked(matchCollection.Count - 1);
        int i = 0;
        while (i <= num)
        {
            using (WebClient webClient = new WebClient())
            {
                webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(this.Complete2);
                webClient.DownloadStringAsync(new Uri(matchCollection[i].Value));
            }
            checked { ++i; }
        }
    } 

【问题讨论】:

  • 你有什么问题。这是一个“问答”网站,而不是“我们为您编写代码”网站
  • 你使用了哪个框架? Netcore 还是 NetFramwork?哪个版本?
  • @Amir 我正在使用 NetFramework。
  • @SOLO 如果你实现了请标记为正确答案。

标签: c# httpclient webclient


【解决方案1】:

你应该这样做:

HttpClient client = new HttpClient();
List<string> urls = new List<string>();
urls.Add("https://www.bing.com/search?q=site:pastebin.com+" + this.textBox1.Text);
urls.Add("https://www.bing.com/search?q=site:pastebin.com+" + this.textBox1.Text);
//and so on...
foreach(var url in urls)
{
   client.GetAsync(url)
           .ContinueWith(async (T) => {
                 string result = await T.Result.ReadAsStringAsync();
                 OnComplete(result);
            });
}

然后:

public void OnComplete(string result)
{
  //todo: convert your result to utf-8 and so on...
}

您可以在 ContinueWith 块中进行一些异常处理以防止错误。

【讨论】:

  • 如果你成功了请告诉我。
  • 亲爱的你应该添加这个包以使用 ReadAsStringAsync 扩展方法:nuget.org/packages/System.Net.Http.Formatting
  • 您可以使用集合初始化语法来初始化您的 URL 列表:var urls = new List&lt;string&gt; { $"http://whatever1/?q=pb+{textBox1.Text}", $"http://whatever2/?q=pb+{textBox1.Text}" };
猜你喜欢
  • 2017-11-23
  • 1970-01-01
  • 2022-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-23
  • 2012-04-25
相关资源
最近更新 更多