• Pricing
Start Now

© 2026 CapSolver. All rights reserved.

CONTACT US

Slack: lola@capsolver.com

Products

  • reCAPTCHA v2
  • reCAPTCHA v3
  • Cloudflare Turnstile
  • Cloudflare Challenge
  • AWS WAF
  • Browser Extension
  • Many more CAPTCHA types

Integrations

  • Selenium
  • Playwright
  • Puppeteer
  • n8n
  • Partners
  • View All Integrations

Resources

  • Referral System
  • Documentation
  • API Reference
  • Blog
  • FAQs
  • Glossary
  • Status

Legal

  • Terms & Conditions
  • Privacy Policy
  • Refund Policy
  • Don't Sell My Info
Blog/All/How to Use HttpClient (C# Library) for Web Scraping
Sep16, 2024

How to Use HttpClient (C# Library) for Web Scraping

Ethan Collins

Ethan Collins

Pattern Recognition Specialist

CAPTCHA challenges, such as Google reCAPTCHA, are commonly used by websites to block bots and prevent automated access to their content. To bypass such challenges programmatically, you can use services like Capsolver that offer API-based solutions to solve these CAPTCHAs.

In this guide, we'll show you how to:

  • Scrape websites using C# HttpClient and HtmlAgilityPack.
  • Solve reCAPTCHA challenges using the Capsolver API.

Web Scraping with C# HttpClient

In C#, the HttpClient class is commonly used to send HTTP requests and receive responses from websites. You can combine this with an HTML parser like HtmlAgilityPack to extract data from web pages.

Prerequisites

  • Install the HtmlAgilityPack library using NuGet Package Manager to help parse HTML content:
bash Copy
Install-Package HtmlAgilityPack
  • Install Newtonsoft.Json to handle JSON responses:
bash Copy
Install-Package Newtonsoft.Json

Example: Scraping "Quotes to Scrape"

Let’s scrape quotes from the Quotes to Scrape website using HttpClient and HtmlAgilityPack.

csharp Copy
using System;
using System.Net.Http;
using System.Threading.Tasks;
using HtmlAgilityPack;

class Program
{
    private static readonly HttpClient client = new HttpClient();

    static async Task Main(string[] args)
    {
        string url = "http://quotes.toscrape.com/";

        // Send a GET request to the page
        HttpResponseMessage response = await client.GetAsync(url);

        if (response.IsSuccessStatusCode)
        {
            // Parse the page content using HtmlAgilityPack
            string pageContent = await response.Content.ReadAsStringAsync();
            HtmlDocument htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(pageContent);

            // Find all the quotes on the page
            var quotes = htmlDoc.DocumentNode.SelectNodes("//span[@class='text']");

            // Print each quote
            foreach (var quote in quotes)
            {
                Console.WriteLine(quote.InnerText);
            }
        }
        else
        {
            Console.WriteLine($"Failed to retrieve the page. Status Code: {response.StatusCode}");
        }
    }
}

Explanation:

  • HttpClient: Sends a GET request to the website.
  • HtmlAgilityPack: Parses the HTML content and extracts quotes by selecting elements with the class text.

Solving reCAPTCHA v3 & reCaptcha v2 with Capsolver using HttpClient

When a website employs reCAPTCHA v3 & reCaptcha v2 for security, you can solve the CAPTCHA using the Capsolver API. Below is how you can integrate Capsolver with HttpClient to solve reCAPTCHA challenges.

Prerequisites

  • Newtonsoft.Json is used to handle JSON parsing from Capsolver responses:
bash Copy
Install-Package Newtonsoft.Json

Example: Solving reCAPTCHA v2 with Capsolver

In this section, we will demonstrate how to solve reCAPTCHA v2 challenges using the Capsolver API and HttpClient.

csharp Copy
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

class Program
{
    private static readonly string apiUrl = "https://api.capsolver.com";
    private static readonly string clientKey = "YOUR_API_KEY"; // Replace with your Capsolver API Key

    static async Task Main(string[] args)
    {
        try
        {
            // Step 1: Create a task for solving reCAPTCHA v3
            string taskId = await CreateTask();
            Console.WriteLine("Task ID: " + taskId);

            // Step 2: Retrieve the result of the task
            string taskResult = await GetTaskResult(taskId);
            Console.WriteLine("Task Result (CAPTCHA Token): " + taskResult);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
    }

    // Method to create a new CAPTCHA-solving task
    private static async Task<string> CreateTask()
    {
        using (HttpClient client = new HttpClient())
        {
            // Request payload
            var requestBody = new
            {
                clientKey = clientKey,
                task = new
                {
                    type = "ReCaptchaV2TaskProxyLess", // Task type for reCAPTCHA v3 without proxy
                    websiteURL = "", // The website URL to solve CAPTCHA for
                    websiteKey = "" // reCAPTCHA site key
                }
            };

            // Send the request to create the task
            var content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(requestBody), Encoding.UTF8, "application/json");
            HttpResponseMessage response = await client.PostAsync($"{apiUrl}/createTask", content);
            string responseContent = await response.Content.ReadAsStringAsync();

            if (!response.IsSuccessStatusCode)
            {
                throw new Exception("Failed to create task: " + responseContent);
            }

            JObject jsonResponse = JObject.Parse(responseContent);
            if (jsonResponse["errorId"].ToString() != "0")
            {
                throw new Exception("Error creating task: " + jsonResponse["errorDescription"]);
            }

            // Return the task ID to be used in the next step
            return jsonResponse["taskId"].ToString();
        }
    }

    // Method to retrieve the result of a CAPTCHA-solving task
    private static async Task<string> GetTaskResult(string taskId)
    {
        using (HttpClient client = new HttpClient())
        {
            // Request payload
            var requestBody = new
            {
                clientKey = clientKey,
                taskId = taskId
            };

            var content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(requestBody), Encoding.UTF8, "application/json");

            // Poll for the result of the task every 5 seconds
            while (true)
            {
                HttpResponseMessage response = await client.PostAsync($"{apiUrl}/getTaskResult", content);
                string responseContent = await response.Content.ReadAsStringAsync();

                if (!response.IsSuccessStatusCode)
                {
                    throw new Exception("Failed to get task result: " + responseContent);
                }

                JObject jsonResponse = JObject.Parse(responseContent);
                if (jsonResponse["errorId"].ToString() != "0")
                {
                    throw new Exception("Error getting task result: " + jsonResponse["errorDescription"]);
                }

                // If the task is ready, return the CAPTCHA token
                if (jsonResponse["status"].ToString() == "ready")
                {
                    return jsonResponse["solution"]["gRecaptchaResponse"].ToString();
                }

                // Wait for 5 seconds before checking again
                Console.WriteLine("Task is still processing, waiting 5 seconds...");
                await Task.Delay(5000);
            }
        }
    }
}

Explanation:

  1. CreateTask Method:

    • This method sends a POST request to Capsolver's /createTask endpoint to create a new task for solving a reCAPTCHA v2 challenge.
    • The request includes the clientKey, websiteURL, websiteKey, and specifies the task type as ReCaptchaV2TaskProxyLess.
    • The method returns a taskId, which will be used to retrieve the task result.
  2. GetTaskResult Method:

    • This method sends a POST request to the /getTaskResult endpoint to check the result of the previously created task.
    • It keeps polling the task status every 5 seconds until the task is completed (status: ready).
    • Once the task is ready, it returns the gRecaptchaResponse, which can be used to bypass the CAPTCHA.

Example: Solving reCAPTCHA v3 with Capsolver

In this section, we will demonstrate how to solve reCAPTCHA v3 challenges using the Capsolver API and HttpClient.

csharp Copy
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

class Program
{
    private static readonly string apiUrl = "https://api.capsolver.com";
    private static readonly string clientKey = "YOUR_API_KEY"; // Replace with your Capsolver API Key

    static async Task Main(string[] args)
    {
        try
        {
            // Step 1: Create a task for solving reCAPTCHA v3
            string taskId = await CreateTask();
            Console.WriteLine("Task ID: " + taskId);

            // Step 2: Retrieve the result of the task
            string taskResult = await GetTaskResult(taskId);
            Console.WriteLine("Task Result (CAPTCHA Token): " + taskResult);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
    }

    // Method to create a new CAPTCHA-solving task
    private static async Task<string> CreateTask()
    {
        using (HttpClient client = new HttpClient())
        {
            // Request payload
            var requestBody = new
            {
                clientKey = clientKey,
                task = new
                {
                    type = "ReCaptchaV3TaskProxyLess", // Task type for reCAPTCHA v3 without proxy
                    websiteURL = "", // The website URL to solve CAPTCHA for
                    websiteKey = "" // reCAPTCHA site key
                }
            };

            // Send the request to create the task
            var content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(requestBody), Encoding.UTF8, "application/json");
            HttpResponseMessage response = await client.PostAsync($"{apiUrl}/createTask", content);
            string responseContent = await response.Content.ReadAsStringAsync();

            if (!response.IsSuccessStatusCode)
            {
                throw new Exception("Failed to create task: " + responseContent);
            }

            JObject jsonResponse = JObject.Parse(responseContent);
            if (jsonResponse["errorId"].ToString() != "0")
            {
                throw new Exception("Error creating task: " + jsonResponse["errorDescription"]);
            }

            // Return the task ID to be used in the next step
            return jsonResponse["taskId"].ToString();
        }
    }

    // Method to retrieve the result of a CAPTCHA-solving task
    private static async Task<string> GetTaskResult(string taskId)
    {
        using (HttpClient client = new HttpClient())
        {
            // Request payload
            var requestBody = new
            {
                clientKey = clientKey,
                taskId = taskId
            };

            var content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(requestBody), Encoding.UTF8, "application/json");

            // Poll for the result of the task every 5 seconds
            while (true)
            {
                HttpResponseMessage response = await client.PostAsync($"{apiUrl}/getTaskResult", content);
                string responseContent = await response.Content.ReadAsStringAsync();

                if (!response.IsSuccessStatusCode)
                {
                    throw new Exception("Failed to get task result: " + responseContent);
                }

                JObject jsonResponse = JObject.Parse(responseContent);
                if (jsonResponse["errorId"].ToString() != "0")
                {
                    throw new Exception("Error getting task result: " + jsonResponse["errorDescription"]);
                }

                // If the task is ready, return the CAPTCHA token
                if (jsonResponse["status"].ToString() == "ready")
                {
                    return jsonResponse["solution"]["gRecaptchaResponse"].ToString();
                }

                // Wait for 5 seconds before checking again
                Console.WriteLine("Task is still processing, waiting 5 seconds...");
                await Task.Delay(5000);
            }
        }
    }
}

Explanation:

  1. CreateTask Method:

    • This method sends a POST request to Capsolver's /createTask endpoint to create a new task for solving a reCAPTCHA v3 challenge.
    • The request includes the clientKey, websiteURL, websiteKey, and specifies the task type as ReCaptchaV3TaskProxyLess.
    • The method returns a taskId, which will be used to retrieve the task result.
  2. GetTaskResult Method:

    • This method sends a POST request to the /getTaskResult endpoint to check the result of the previously created task.
    • It keeps polling the task status every 5 seconds until the task is completed (status: ready).
    • Once the task is ready, it returns the gRecaptchaResponse, which can be used to bypass the CAPTCHA.

Bonus Code

Claim Your Bonus Code for top captcha solutions; CapSolver: scrape. After redeeming it, you will get an extra 5% bonus after each recharge, Unlimited

Web Scraping Best Practices in C#

When using web scraping tools in C#, always follow these best practices:

  • Respect robots.txt: Ensure that the website allows web scraping by checking the robots.txt file.
  • Rate Limiting: Avoid making too many requests in a short period to prevent getting blocked by the website.
  • Proxy Rotation: Use proxies to distribute requests across multiple IPs to avoid being flagged as a bot.
  • Spoof Headers: Simulate browser-like requests by adding custom headers, such as User-Agent, to your HTTP requests.

Conclusion

By using HttpClient for web scraping and Capsolver for CAPTCHA solving, you can effectively automate interactions with websites that employ CAPTCHA challenges. Always ensure that your web scraping activities comply with the target website's terms of service and legal requirements.

Happy scraping!


This guide integrates web scraping using HtmlAgilityPack and demonstrates how to handle reCAPTCHA challenges with Capsolver, using only HttpClient in C#.

More

AIMay 08, 2026

How AI Data Extraction Works: CAPTCHA Solving, LLM Parsing & Structured Web Data Pipelines

Learn how AI-powered data extraction works from web scraping and CAPTCHA solving to HTML cleaning, LLM parsing, and structured JSON generation. Explore anti-bot bypass strategies, semantic extraction frameworks like AXE, and scalable AI web scraping pipelines.

Lucas Mitchell
Lucas Mitchell
reCAPTCHAMay 08, 2026

ReCAPTCHA in Ecommerce Scraping: A Compliance-First Guide

Learn how to handle recaptcha during ecommerce scraping with compliant workflows, diagnostics, CapSolver examples, and practical risk controls.

Contents

Rajinder Singh
Rajinder Singh
AIMay 06, 2026

How to Solve CAPTCHA in Browser Automation with Hermes Agent and CapSolver

Learn how to solve CAPTCHA in AI browser automation workflows using Hermes Agent and CapSolver. This guide explains how to integrate CapSolver to automatically handle reCAPTCHA, hCaptcha, and other modern CAPTCHA systems in automated browsing environments without writing complex code.

Ethan Collins
Ethan Collins
AIMay 07, 2026

Best AI Agent Frameworks for Web Automation and CAPTCHA Solving

Compare the best ai agent frameworks for web automation, CAPTCHA solving, compliance, and production-ready agent workflows in 2026.

Sora Fujimoto
Sora Fujimoto