Table of contents

How do I configure proxy settings for HttpClient (C#)?

Configuring proxy settings for HttpClient in C# is essential when you need to route HTTP requests through a proxy server for security, anonymity, or network access requirements.

Basic Proxy Configuration

Use HttpClientHandler with the Proxy property to configure proxy settings:

using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

public class HttpClientProxyExample
{
    public static HttpClient CreateHttpClientWithProxy()
    {
        // Configure proxy settings
        var proxy = new WebProxy("http://proxy-server.com:8080")
        {
            // Optional: Set credentials for authenticated proxies
            Credentials = new NetworkCredential("username", "password"),

            // Optional: Bypass proxy for local addresses
            BypassProxyOnLocal = true
        };

        // Create handler with proxy configuration
        var handler = new HttpClientHandler()
        {
            Proxy = proxy,
            UseProxy = true
        };

        return new HttpClient(handler);
    }
}

Different Proxy Types

HTTP Proxy

var proxy = new WebProxy("http://proxy.example.com:8080");

HTTPS Proxy

var proxy = new WebProxy("https://secure-proxy.example.com:443");

SOCKS Proxy (via SocketsHttpHandler)

// For .NET 5+ and SOCKS proxy support
var handler = new SocketsHttpHandler()
{
    Proxy = new WebProxy("socks5://socks-proxy.example.com:1080")
};
var client = new HttpClient(handler);

Advanced Configuration Options

Proxy with Authentication

public static HttpClient CreateAuthenticatedProxyClient()
{
    var proxy = new WebProxy("http://proxy.example.com:8080")
    {
        Credentials = new NetworkCredential("proxyUser", "proxyPass"),

        // Use default credentials (current Windows user)
        // Credentials = CredentialCache.DefaultCredentials
    };

    var handler = new HttpClientHandler()
    {
        Proxy = proxy,
        UseProxy = true,
        PreAuthenticate = true // Send credentials with first request
    };

    return new HttpClient(handler);
}

Bypass Proxy for Specific Addresses

var proxy = new WebProxy("http://proxy.example.com:8080")
{
    BypassProxyOnLocal = true,

    // Bypass proxy for specific addresses
    BypassList = new[] { 
        "*.local.com",
        "192.168.*",
        "localhost"
    }
};

System Proxy Configuration

// Use system proxy settings
var handler = new HttpClientHandler()
{
    UseProxy = true,
    Proxy = null // Uses system default proxy
};

var client = new HttpClient(handler);

Complete Example with Error Handling

using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

public class ProxyHttpClientService
{
    private readonly HttpClient _httpClient;

    public ProxyHttpClientService(string proxyUrl, string username = null, string password = null)
    {
        var proxy = new WebProxy(proxyUrl);

        if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
        {
            proxy.Credentials = new NetworkCredential(username, password);
        }

        var handler = new HttpClientHandler()
        {
            Proxy = proxy,
            UseProxy = true,
            ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true // Only for testing
        };

        _httpClient = new HttpClient(handler)
        {
            Timeout = TimeSpan.FromSeconds(30)
        };
    }

    public async Task<string> GetAsync(string url)
    {
        try
        {
            var response = await _httpClient.GetAsync(url);
            response.EnsureSuccessStatusCode();
            return await response.Content.ReadAsStringAsync();
        }
        catch (HttpRequestException ex)
        {
            throw new Exception($"HTTP request failed: {ex.Message}", ex);
        }
        catch (TaskCanceledException ex)
        {
            throw new Exception($"Request timeout: {ex.Message}", ex);
        }
    }

    public void Dispose()
    {
        _httpClient?.Dispose();
    }
}

// Usage example
class Program
{
    static async Task Main(string[] args)
    {
        var service = new ProxyHttpClientService(
            "http://proxy.example.com:8080", 
            "username", 
            "password"
        );

        try
        {
            string content = await service.GetAsync("https://api.example.com/data");
            Console.WriteLine(content);
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error: {ex.Message}");
        }
        finally
        {
            service.Dispose();
        }
    }
}

Using Dependency Injection (ASP.NET Core)

// In Startup.cs or Program.cs
services.AddHttpClient("ProxyClient", client =>
{
    // Configure client settings
})
.ConfigurePrimaryHttpMessageHandler(() =>
{
    var proxy = new WebProxy("http://proxy.example.com:8080")
    {
        Credentials = new NetworkCredential("username", "password")
    };

    return new HttpClientHandler()
    {
        Proxy = proxy,
        UseProxy = true
    };
});

// In your service or controller
public class MyService
{
    private readonly HttpClient _httpClient;

    public MyService(IHttpClientFactory httpClientFactory)
    {
        _httpClient = httpClientFactory.CreateClient("ProxyClient");
    }
}

Important Considerations

  1. HttpClient Reuse: Always reuse HttpClient instances to avoid socket exhaustion
  2. Disposal: Properly dispose of HttpClient and HttpClientHandler when done
  3. Timeout Configuration: Set appropriate timeouts for proxy connections
  4. Security: Never hardcode credentials in production code
  5. SSL Validation: Be cautious when bypassing SSL certificate validation
  6. Proxy Authentication: Test authentication requirements with your proxy provider

Common Proxy Configuration Scenarios

  • Corporate Networks: Use system proxy with Windows authentication
  • Web Scraping: Rotate between multiple proxy servers for rate limiting
  • Security Testing: Route traffic through security analysis proxies
  • Geographic Testing: Use proxies in different regions for location-based testing

Try WebScraping.AI for Your Web Scraping Needs

Looking for a powerful web scraping solution? WebScraping.AI provides an LLM-powered API that combines Chromium JavaScript rendering with rotating proxies for reliable data extraction.

Key Features:

  • AI-powered extraction: Ask questions about web pages or extract structured data fields
  • JavaScript rendering: Full Chromium browser support for dynamic content
  • Rotating proxies: Datacenter and residential proxies from multiple countries
  • Easy integration: Simple REST API with SDKs for Python, Ruby, PHP, and more
  • Reliable & scalable: Built for developers who need consistent results

Getting Started:

Get page content with AI analysis:

curl "https://api.webscraping.ai/ai/question?url=https://example.com&question=What is the main topic?&api_key=YOUR_API_KEY"

Extract structured data:

curl "https://api.webscraping.ai/ai/fields?url=https://example.com&fields[title]=Page title&fields[price]=Product price&api_key=YOUR_API_KEY"

Try in request builder

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon