Skip to content
.NET HttpClient

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

HttpClient takes its proxy from the handler, not from the request, so the pattern is one handler (and one long-lived client) per proxy. That also gives each proxy its own connection pool.
  • 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

Every CleanProxies product works over HTTP(S) and SOCKS5. This is what the dashboard gives you.
ProductLine in your dashboardNotes
Static ISP proxiesIP:PORT:USER:PASSOne line per dedicated IP. With IP whitelisting turned on, the line is just IP:PORT and no password is sent.
Rotating residential and mobileHOST:PORT:USER:PASSGenerated 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. 1

    Create a WebProxy

    Use http://HOST:PORT for HTTP(S) or socks5://HOST:PORT for SOCKS5, and attach a NetworkCredential with your username and password.
  2. 2

    Attach it to a handler

    Set Proxy and UseProxy = true on an HttpClientHandler (or SocketsHttpHandler).
  3. 3

    Reuse the client

    Create the HttpClient once per proxy and reuse it; creating one per request exhausts sockets.
  4. 4

    Check the exit IP

    Call GetStringAsync("https://api.ipify.org") and log the result.

HttpClient with an authenticated proxy

HTTP(S) proxyC#
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}");
SOCKS5 (.NET 6 and later)C#
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.

Program.csC#
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

Both products use the same setup. What changes is how long an IP stays with you.

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.

Static ISP ProxiesAs low as $0.75/IP

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.

Rotating Residential ProxiesAs low as $0.63/GB

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

ProblemLikely causeFix
407 Proxy Authentication RequiredCredentials set on the handler instead of the WebProxy, or a typo.Put the NetworkCredential on WebProxy.Credentials.
TaskCanceledException after 100 secondsDefault 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 establishedTLS 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 useA 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.

Get CleanProxies for C# HttpClient

Super clean US ISP IPs from $0.75/IP with unlimited bandwidth, rotating residential from $0.63/GB and mobile from $0.79/GB. Instant delivery, 99.9% uptime and 24/7 support.