C# HttpClient Proxy Setup
C#'s HttpClient is the standard HTTP client for .NET applications. It supports proxy configuration through HttpClientHandler, providing clean integration with Hex Proxies for web scraping, API access, and data collection in .NET projects. The key to reliable proxy usage in .NET is proper HttpClient lifecycle management — creating a single client instance and reusing it.
Complete Configuration with Authentication
using System.Net;var proxyUser = Environment.GetEnvironmentVariable("PROXY_USER"); var proxyPass = Environment.GetEnvironmentVariable("PROXY_PASS");
var proxy = new WebProxy("http://gate.hexproxies.com:8080", false) { Credentials = new NetworkCredential(proxyUser, proxyPass) };
var handler = new HttpClientHandler { Proxy = proxy, UseProxy = true, AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate, MaxConnectionsPerServer = 20, };
var client = new HttpClient(handler) { Timeout = TimeSpan.FromSeconds(30) };
client.DefaultRequestHeaders.UserAgent.ParseAdd( "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"); client.DefaultRequestHeaders.AcceptLanguage.ParseAdd("en-US,en;q=0.9");
var response = await client.GetAsync("https://httpbin.org/ip"); var body = await response.Content.ReadAsStringAsync(); Console.WriteLine($"[{(int)response.StatusCode}] {body}"); ```
Using IHttpClientFactory (Recommended for ASP.NET)
In ASP.NET Core applications, use IHttpClientFactory with named clients for proper lifecycle management:
// In Startup.cs or Program.cs
builder.Services.AddHttpClient("proxied", client =>
{
client.Timeout = TimeSpan.FromSeconds(30);
client.DefaultRequestHeaders.UserAgent.ParseAdd(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
})
.ConfigurePrimaryHttpMessageHandler(() =>
{
var proxy = new WebProxy("http://gate.hexproxies.com:8080")
{
Credentials = new NetworkCredential(
Environment.GetEnvironmentVariable("PROXY_USER"),
Environment.GetEnvironmentVariable("PROXY_PASS"))
};
return new HttpClientHandler { Proxy = proxy, UseProxy = true };// In your service class public class MyService { private readonly HttpClient _client; public MyService(IHttpClientFactory factory) { _client = factory.CreateClient("proxied"); } } ```
Geo-Targeted Requests
Modify the proxy credentials per request for geo-targeting:
var geoProxy = new WebProxy("http://gate.hexproxies.com:8080")
{
Credentials = new NetworkCredential(
proxyUser + "-country-de", proxyPass)
};Common Pitfalls
The biggest .NET mistake is creating a new HttpClient per request. This causes socket exhaustion because disposed HttpClients leave sockets in TIME_WAIT state. Always reuse HttpClient instances or use IHttpClientFactory. Another issue: HttpClientHandler does not support changing the proxy after client creation. For dynamic proxy switching, create separate handler/client pairs per proxy configuration.
Retry with Polly
For production proxy usage, add Polly retry policies:
builder.Services.AddHttpClient("proxied")
.AddTransientHttpErrorPolicy(p =>
p.WaitAndRetryAsync(3,
attempt => TimeSpan.FromSeconds(attempt * 2)));This automatically retries on 5xx errors and network failures with exponential backoff.