Search code examples
c#httpclientwebclient

Converting WebClient code to HttpClient code


I need help converting this WebClient to HttpClient code if it's possible, I will be really happy if it is.

I've been looking for something/someone that will convert it to me and I sadly didn't find anything.

Thanks!

    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; }
        }
    } 

Solution

  • You should do something like this:

    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);
                });
    }
    

    Then:

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

    You can do some exception handling inside ContinueWith block to prevent errors.