v1.8.91-d84675c
← Back to Hex Proxies

C# HttpClient Proxy Setup

Last updated: April 2026

By Hex Proxies Engineering Team

Route C# HTTP requests through Hex Proxies using HttpClient and WebProxy. Covers authentication, dependency injection with IHttpClientFactory, and retry policies.

intermediate15 minuteslanguage-integration

Prerequisites

  • .NET 6 or later
  • Hex Proxies account with credentials

Steps

1

Create a WebProxy instance

Point WebProxy to gate.hexproxies.com:8080 with NetworkCredential for authentication.

2

Configure HttpClientHandler

Set the Proxy property and UseProxy to true on the handler.

3

Build the HttpClient

Instantiate HttpClient with the configured handler and set a timeout.

4

Test the connection

Send a GET to httpbin.org/ip and verify the proxied IP address.

5

Add retry policies

Use Polly to add exponential backoff for transient errors and 429 responses.

6

Use IHttpClientFactory

Register the proxied client in DI for proper connection pooling and lifetime management.

C# HttpClient Proxy Integration

The .NET HttpClient supports proxies through HttpClientHandler and WebProxy. For production applications, IHttpClientFactory provides pooled, configurable clients with proxy support.

Basic Proxy Setup

var proxy = new WebProxy("http://gate.hexproxies.com:8080") { Credentials = new NetworkCredential("YOUR_USERNAME", "YOUR_PASSWORD") };

var handler = new HttpClientHandler { Proxy = proxy, UseProxy = true };

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

var response = await client.GetAsync("https://httpbin.org/ip"); var body = await response.Content.ReadAsStringAsync(); Console.WriteLine($"Status: {response.StatusCode}"); Console.WriteLine($"Body: {body}"); ```

Sticky Sessions

var stickyProxy = new WebProxy("http://gate.hexproxies.com:8080")
{
    Credentials = new NetworkCredential(
        "YOUR_USERNAME-session-dotnet001",
        "YOUR_PASSWORD"
    )
};

IHttpClientFactory with Proxy (Dependency Injection)

// In Program.cs or Startup.cs
builder.Services.AddHttpClient("proxied", client =>
{
    client.Timeout = TimeSpan.FromSeconds(30);
})
.ConfigurePrimaryHttpMessageHandler(() =>
{
    var proxy = new WebProxy("http://gate.hexproxies.com:8080")
    {
        Credentials = new NetworkCredential(
            Environment.GetEnvironmentVariable("HEX_PROXY_USER"),
            Environment.GetEnvironmentVariable("HEX_PROXY_PASS")
        )

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

// In a service class public class ScrapingService { private readonly HttpClient _client;

public ScrapingService(IHttpClientFactory factory) { _client = factory.CreateClient("proxied"); }

public async Task<string> FetchAsync(string url) { var response = await _client.GetAsync(url); response.EnsureSuccessStatusCode(); return await response.Content.ReadAsStringAsync(); } } ```

Retry with Polly

using Polly;

builder.Services.AddHttpClient("proxied") .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler { Proxy = new WebProxy("http://gate.hexproxies.com:8080") { Credentials = new NetworkCredential("YOUR_USERNAME", "YOUR_PASSWORD") }, UseProxy = true }) .AddPolicyHandler( HttpPolicyExtensions .HandleTransientHttpError() .OrResult(r => r.StatusCode == (HttpStatusCode)429) .WaitAndRetryAsync(3, attempt => TimeSpan.FromSeconds(Math.Pow(2, attempt))) ); ```

Concurrent Requests

var urls = new[]
{
    "https://httpbin.org/ip",
    "https://httpbin.org/headers",
    "https://httpbin.org/get"

var tasks = urls.Select(async url => { var resp = await client.GetAsync(url); var body = await resp.Content.ReadAsStringAsync(); return (url, resp.StatusCode, body); });

var results = await Task.WhenAll(tasks); foreach (var (url, status, body) in results) { Console.WriteLine($"{url}: {status}"); } ```

SOCKS5 via SocketsHttpHandler (.NET 6+)

var handler = new SocketsHttpHandler
{
    Proxy = new WebProxy("socks5://gate.hexproxies.com:1080")
    {
        Credentials = new NetworkCredential("YOUR_USERNAME", "YOUR_PASSWORD")
    },
    UseProxy = true
};

Tips

  • *Always use IHttpClientFactory in ASP.NET Core apps to avoid socket exhaustion.
  • *Polly integrates directly with IHttpClientFactory for resilient proxy requests.
  • *For SOCKS5 on .NET 6+, use SocketsHttpHandler instead of HttpClientHandler.
  • *Read credentials from environment variables or IConfiguration, never hardcode them.

Ready to Get Started?

Put this guide into practice with Hex Proxies.

Cookie Preferences

We use cookies to ensure the best experience. You can customize your preferences below. Learn more