Skip to content

C# HttpClient Proxy Setup: Basic, Authenticated, and Rotating

HttpClient is the standard .NET client for HTTP calls, and it can be pointed at a proxy without changing how you send requests. The pattern is short: build a WebProxy, attach it to an HttpClientHandler, and hand that handler to HttpClient. The parts that trip people up are credentials, rotation, and certificate validation. This guide walks through each in turn and ends with a decision table for picking an approach.

Quick start: the shortest working setup

The minimum viable configuration is a WebProxy instance assigned to an HttpClientHandler, which is then passed into the HttpClient constructor. The starter snippet below also sets a short timeout and calls a public IP echo service so you can see which address the request exits from.

What HttpClient does

HttpClient lives in System.Net.Http and sends HTTP requests and reads responses. It is async-first, so awaiting a call does not block the thread while the network works. Headers, handlers, logging, retries, and proxies are all configurable through the handler pipeline.

What a proxy adds to the path

A proxy server sits between your application and the destination. Requests are routed through it rather than sent directly, which can hide your real IP, make traffic appear from another region, or add a control layer. Proxies may be open or require authentication, and they may support only HTTP, HTTPS, or broader tunneling such as SOCKS5. In C# terms, configuring a WebProxy tells HttpClient to send traffic through that address instead of straight to the internet.

Why pair HttpClient with a proxy

Proxies give you control over how requests leave your network. Common reasons are anonymity, avoiding rate limits tied to your real IP, and reaching APIs that only respond in certain regions. In scraping jobs, rotating proxies keep a run going instead of stalling after a few requests. Spreading traffic across IPs also reduces bans and captchas. Free proxies are usually slow, unstable, or already burned; paid providers and managed scraping APIs supply cleaner pools without the upkeep.

Create a console project

You do not need a heavy IDE. Install the .NET SDK, confirm the version, then scaffold a console app and open it in your editor. The commands below create a project called HttpClientProxyDemo.

Confirm a direct request works first

Before adding a proxy, verify the project can reach the network. The snippet creates an HttpClient, calls GetStringAsync against https://api.ipify.org/, and prints the result. That service returns your public IP as plain text, so the output is your baseline. Run the project and note the IP; after adding a proxy, the same call should print a different address.

Route requests through a proxy

The flow has three steps: create a WebProxy with the proxy address, assign it to an HttpClientHandler, and pass that handler into HttpClient. The example below uses an open HTTP proxy with no authentication. If the proxy is live, the IP echo service returns the proxy's exit address instead of yours. Free proxies are fine for a quick experiment but fail unpredictably; the next sections cover authenticated and rotating options.

Add credentials to the proxy

Most paid residential and datacenter providers require a username and password. The setup is nearly identical: create the WebProxy, then set its Credentials property to a NetworkCredential holding the username and password pair. The handler forwards authentication automatically. When you run it, the printed IP should match the proxy's exit node. This configuration is more stable than random free proxies and produces fewer connection surprises.

Rotate proxies across requests

Reusing one proxy for many requests can get that IP flagged quickly. There are two rotation patterns. The simple one creates a new HttpClient per request, which is heavier but easy to reason about. The scalable one keeps a pool of clients, each bound to its own handler and proxy, and can be managed with IHttpClientFactory. For most scraping or batch jobs, the per-request approach is fine unless you are pushing thousands of requests per second. Some providers also expose a single endpoint that rotates IPs behind the scenes; in that case your HttpClient code does not change at all.

Use a managed proxy endpoint

A managed proxy service removes the need to maintain a list. You point HttpClient at a single HTTPS proxy endpoint, pass your API key and parameters in the URL, and the service handles rotation behind that endpoint. The example below reads the key from an environment variable and disables JS rendering by default. Several operational details depend on the provider you use. Whether proxy mode requires disabling SSL verification depends on whether TLS terminates at the proxy ({{OWNER_SSL_TERMINATION}}). Whether IP rotation happens automatically per request is a provider behavior ({{OWNER_ROTATION_BEHAVIOR}}). Header forwarding uses {{OWNER_HEADER_FORWARDING_PARAM}}. When proxy mode is combined with headless browsers, every asset request may consume credits ({{OWNER_ASSET_BILLING}}), so trim images and fonts where possible. Scraping Google through proxy mode costs {{OWNER_GOOGLE_CREDIT_COST}} per request.

Handler settings that matter

HttpClientHandler exposes a handful of options that change how proxying behaves. The table below maps each one to what it does and what it does not do. The most common mistake is expecting PreAuthenticate to help with proxy authentication; it only affects authentication to the target server, while proxy (407) authentication goes through WebProxy.Credentials.

When to use SocketsHttpHandler

On modern .NET, HttpClientHandler wraps a SocketsHttpHandler internally. If you manage multiple clients in production, configuring SocketsHttpHandler directly gives more control over connection pooling, DNS refresh, and proxy lifetime. The example below sets a five-minute pooled connection lifetime and enables gzip and deflate decompression.

SSL validation: when to disable it and when not to

Disabling certificate checks appears in many proxy examples, but the reason matters. There are three common situations, and only one justifies turning validation off.

TLS-terminating proxy (managed HTTPS proxy mode). Here the proxy itself is the TLS endpoint. Your client completes a secure handshake with the proxy, not with the destination site, so the certificate it sees is issued for the proxy host rather than the target domain. .NET's default validation rejects that as a hostname mismatch. Because you already authenticate to the proxy endpoint with your API key, turning validation off in this specific case is the expected trade-off.

Free or misconfigured proxies. These often break TLS accidentally through self-signed certificates, wrong CN/SAN values, or incomplete chains. Disabling validation in this case lets your client accept a forged certificate and complete TLS with the proxy instead of the real destination, effectively turning the proxy into a man-in-the-middle that can read or alter HTTPS traffic. Use this only for demos, never in production.

CONNECT or SOCKS proxies. TLS passes through end-to-end, so no bypass is needed. If validation fails here, it usually points to a misconfigured proxy or an invalid server certificate. Some managed providers also offer a SOCKS proxy mode if you prefer end-to-end TLS ({{OWNER_SOCKS_SUPPORT}}).

For production, prefer valid certificate chains, pinned certificates or trusted CA bundles, and failing closed rather than silently bypassing. A controlled callback that only allows SslPolicyErrors.None is a safer default than a blanket bypass.

Retry with backoff

Rotating proxies eventually hit a slow or dead endpoint. A retry with exponential backoff keeps one bad proxy from failing a whole job. The pattern is: try, wait, retry with a longer delay each time; skip proxies that consistently fail; and log which proxies perform well. You can write this yourself or use a library such as Polly. The example below retries up to three times, waiting 1s, 2s, then 4s, and handles timeouts, connection errors, and non-success status codes. If the request still fails after retries, it errors out. A short timeout makes dead proxies surface faster.

Performance expectations

Proxies add overhead because requests travel through another server. Direct HttpClient is fastest; authenticated or personal proxies are slightly slower; rotating, residential, and premium proxies are slowest but most reliable. In scraping, reliability usually beats raw speed. A request that returns correctly in 600ms is better than one that fails instantly and needs three retries. At scale, stable success rates outperform micro-optimized latency.

Choosing an approach

Use the following decision table to match the approach to the job. System or free proxies are fine for demos and snippets but not real workloads. Authenticated proxies suit small tools and steady traffic with one stable endpoint. DIY rotation works but brings failure handling, retries, health checks, region targeting, and scaling problems. A managed service can handle rotation, geolocation ({{OWNER_GEOLOCATION}}), premium clean IPs ({{OWNER_PREMIUM_PROXY_PARAM}}), and optional JS rendering ({{OWNER_RENDER_JS_PARAM}}), and fits any meaningful scraping scale.

Putting it together

You now have the full path: a basic WebProxy setup, credentials through NetworkCredential, manual rotation across a list, and a managed endpoint that rotates for you. Free proxies are fine for tests but fall apart in real jobs. Authenticated proxies are more stable but still leave rotation, region targeting, and retries to you. A managed service removes most of that overhead. The setups above are enough to build more reliable scraping workflows in C#.

HttpClientHandler proxy options
OptionWhat it does
UseProxyEnables or disables using a proxy entirely. Useful when toggling behavior at runtime.
ProxyAssigns the proxy implementation (WebProxy, custom proxy, etc.).
UseDefaultCredentialsTells the client to use the machine's default credentials (usually not what you want with external proxies).
PreAuthenticateSends auth headers early for the target server (not the proxy). It helps skip an extra 401 round-trip on server auth. It does nothing for proxy (407) authentication; that's handled through WebProxy.Credentials.

This table describes HttpClientHandler options as documented at the time of writing. Behavior may vary across .NET versions; check the current API reference for your target framework.

Frequently asked questions

Can I use an authenticated proxy with HttpClient?

Yes. Create a WebProxy, assign a NetworkCredential to its Credentials property, and attach the proxy to an HttpClientHandler. The handler sends the authentication details automatically when making requests.

Can I rotate proxies in HttpClient automatically?

Not by default. HttpClient does not rotate proxies on its own. You either switch proxies manually from a list or use a service that rotates IPs behind a single endpoint.

How do I handle SSL errors when using a proxy in C#?

There are two common causes. With a TLS-terminating proxy such as a managed HTTPS proxy mode, the certificate will not match the target host, so disabling validation is expected because you trust the proxy endpoint. With standard CONNECT or SOCKS proxies, TLS is end-to-end and validation should stay on. In production, prefer valid certificate chains or explicit validation callbacks over blanket bypasses.

Does HttpClient use system proxy settings by default?

Yes. If you do not specify a proxy handler, HttpClient uses the system's default proxy settings. You can override this by setting UseProxy = false or by supplying your own WebProxy through HttpClientHandler.Proxy.

What's the best proxy approach for production web scraping?

Managed proxy services are the most reliable option for scraping at scale. They provide automatic IP rotation, geolocation, and stable performance without maintaining proxy lists yourself.

Should I reuse a single HttpClient instance?

Yes. Reusing HttpClient prevents socket exhaustion and improves performance. However, you cannot swap handlers or proxies on an existing client because each one is bound to its handler at creation. If you need different proxies, keep a small pool of preconfigured clients or use IHttpClientFactory to manage them.

How do I set a request timeout?

Set client.Timeout to a suitable duration or pass a CancellationToken. Shorter timeouts help detect dead proxies quickly and make retry logic more effective.

Why does my request fail with a hostname mismatch when using a proxy?

A hostname mismatch usually means the proxy terminates TLS itself. The certificate you receive belongs to the proxy, not the destination domain, so .NET rejects it. This is expected with a TLS-terminating proxy you trust. With a CONNECT or SOCKS proxy, TLS passes through end-to-end and a mismatch points to a misconfigured proxy or an invalid server certificate.

Does PreAuthenticate help with proxy authentication?

No. PreAuthenticate sends authentication headers early for the target server and can skip an extra 401 round-trip. Proxy authentication (407) is handled through WebProxy.Credentials, not PreAuthenticate.