How to use proxies in C# with HttpClient
In C#, create a WebProxy("http://HOST:PORT") with Credentials = new NetworkCredential("USERNAME", "PASSWORD"), assign it to an HttpClientHandler, and build your HttpClient from that handler. .NET 6 and later also accept a socks5://HOST:PORT address for SOCKS5.
What you need
- An active CleanProxies plan: static ISP proxies for fixed US IPs, or rotating residential / mobile proxies for pools. The trial works too.
- Your credential line from the dashboard's proxy manager: host (or IP), port, username and password.
- The .NET 8 SDK or later (HttpClient behaves the same from .NET 6).
- A console or ASP.NET Core project.
New to proxies? For the overview, read Using proxies with cURL, Python and Node.js and SOCKS5 vs HTTP proxies. This guide goes deeper on C# HttpClient only.
Your CleanProxies credentials
| Product | Line in your dashboard | Notes |
|---|---|---|
| Static ISP proxies | IP:PORT:USER:PASS | One line per dedicated IP. With IP whitelisting turned on, the line is just IP:PORT and no password is sent. |
| Rotating residential and mobile | HOST:PORT:USER:PASS | Generated in the proxy manager. The username carries the location and session you pick, so generate a new list to change them. |
Most tools want the same details as a URL: http://USERNAME:PASSWORD@HOST:PORT for HTTP(S) or socks5://USERNAME:PASSWORD@HOST:PORT for SOCKS5. The examples on this page use those placeholders; copy the host, port, username and password from your CleanProxies dashboard. To convert a whole list at once, paste it into the proxy list formatter.
How to set up CleanProxies in C# HttpClient
- 1
Create a WebProxy
Usehttp://HOST:PORTfor HTTP(S) orsocks5://HOST:PORTfor SOCKS5, and attach aNetworkCredentialwith your username and password. - 2
Attach it to a handler
SetProxyandUseProxy = trueon anHttpClientHandler(orSocketsHttpHandler). - 3
Reuse the client
Create theHttpClientonce per proxy and reuse it; creating one per request exhausts sockets. - 4
Check the exit IP
CallGetStringAsync("https://api.ipify.org")and log the result.
HttpClient with an authenticated proxy
using System.Net;
var proxy = new WebProxy("http://HOST:PORT")
{
Credentials = new NetworkCredential("USERNAME", "PASSWORD")
};
var handler = new HttpClientHandler
{
Proxy = proxy,
UseProxy = true
};
using var client = new HttpClient(handler) { Timeout = TimeSpan.FromSeconds(30) };
var ip = await client.GetStringAsync("https://api.ipify.org");
Console.WriteLine($"Exit IP: {ip}");var socks = new WebProxy("socks5://HOST:PORT")
{
Credentials = new NetworkCredential("USERNAME", "PASSWORD")
};
var handler = new SocketsHttpHandler { Proxy = socks, UseProxy = true };
using var client = new HttpClient(handler);ASP.NET Core: a named client per proxy
In ASP.NET Core, register the proxy on the primary handler of a named client so the factory manages handler lifetimes for you.
builder.Services.AddHttpClient("cleanproxies", client =>
{
client.Timeout = TimeSpan.FromSeconds(30);
})
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
{
Proxy = new WebProxy("http://HOST:PORT")
{
Credentials = new NetworkCredential("USERNAME", "PASSWORD")
},
UseProxy = true
});
// later: var client = httpClientFactory.CreateClient("cleanproxies");Keep the username and password in user secrets or environment variables, not in appsettings.json in source control.
Static ISP vs rotating residential in C# HttpClient
Static ISP proxies
One handler per ISP line means one dedicated US IP per client. Pooled connections stay on that IP, which is exactly what logged-in or API-style traffic wants.
Rotating residential proxies
Pooled connections stay on the exit IP they opened with. For per-request rotation on a rotating residential line, set PooledConnectionLifetime low on a SocketsHttpHandler or send Connection: close; for sticky flows, keep the defaults.
Verify your exit IP
Log the result of GetStringAsync("https://api.ipify.org") at startup. If it matches your own server's IP, the handler isn't using the proxy (check UseProxy and that you built the client from that handler).
Open What is my IP? through the proxy to see the address and headers a website receives, or paste up to ten lines into the free proxy checker to test connectivity, latency and exit IP before you start.
Troubleshooting C# HttpClient proxy errors
| Problem | Likely cause | Fix |
|---|---|---|
| 407 Proxy Authentication Required | Credentials set on the handler instead of the WebProxy, or a typo. | Put the NetworkCredential on WebProxy.Credentials. |
| TaskCanceledException after 100 seconds | Default timeout hit because the proxy port is unreachable. | Check host and port with curl -x; set a shorter Timeout so failures surface quickly. |
| The SSL connection could not be established | TLS interception on the network or an https:// proxy address. | Use http://HOST:PORT for the proxy address. Don't bypass certificate validation. |
| SocketException: address already in use | A new HttpClient per request. | Reuse clients or use IHttpClientFactory. |
Still stuck? Our 24/7 support team answers fast: contact us with the error and the line you used (leave the password out).
C# HttpClient proxy FAQs
Yes. Since .NET 6, SocketsHttpHandler (which HttpClientHandler uses) accepts socks4, socks4a and socks5 proxy addresses through WebProxy, with username and password credentials for SOCKS5.