Proxy setup in C# (.NET)

Proxies in C# and .NET: HttpClientHandler with WebProxy, credentials via NetworkCredential, SOCKS5 since .NET 6 and one HttpClient for the whole app.

Updated · Prokso editorial team

Markdown, ready to paste into ChatGPT or another AI assistant
Connection details
Host
gw.prokso.com
Port
823 HTTP · 824 SOCKS5 - new IP per request · 10000–20000 sticky session, HTTP(S) and SOCKS5
Username and password
in your dashboard, on the plan page

Your dashboard generates ready-made lines in the format you need: choose the country, IP type and protocol.

Key takeaways
  • The proxy is set in SocketsHttpHandler via WebProxy, the username goes in Credentials
  • One HttpClient per proxy port; for several addresses, several clients
  • SOCKS5 since .NET 6, address as the string socks5://gw.prokso.com:824

In .NET the proxy is set not per request but per handler: HttpClient gets it from SocketsHttpHandler or HttpClientHandler, and they get it from a WebProxy object. So the main decision here is how many clients to create and how to keep them. The examples below target .NET 6 and later; SOCKS5 in HttpClient arrived in .NET 6.

Step 1. Build the proxy address for HttpClient

WebProxy takes the address as a URI, and the username and password separately, in Credentials. So from the dashboard you need two things: a string like http://gw.prokso.com:823 and the username and password from the “Access” block on the plan page.

The port defines the behavior: 823 is HTTP with a new IP per request, 824 is SOCKS5 with the same rotation, and ports 10000-20000 give a sticky IP for 1-120 minutes (each port is its own address and works as both HTTP and SOCKS5). The country is set by the username: generate a string for the country you need in the dashboard and take the username from it.

Microsoft warns specifically that the WebProxy(string host, int port) constructor always builds an address with the http:// scheme. That is fine for an HTTP proxy but not for SOCKS5, so it is better to write the address as a full URI string from the start.

Step 2. WebProxy, NetworkCredential and SocketsHttpHandler

A complete example: a proxy with a username, a handler with a connection pool, and a request that prints the IP the site sees your program coming from.

using System.Net;

var proxy = new WebProxy("http://gw.prokso.com:823")
{
    Credentials = new NetworkCredential("USERNAME", "PASSWORD")
};

var handler = new SocketsHttpHandler
{
    Proxy = proxy,
    UseProxy = true,
    PooledConnectionLifetime = TimeSpan.FromMinutes(5)
};

// one client for the whole app
var client = new HttpClient(handler) { Timeout = TimeSpan.FromSeconds(30) };

string ip = await client.GetStringAsync("https://api.ipify.org");
Console.WriteLine(ip);   // prints the IP the site sees

Credentials in WebProxy are the data .NET sends to the proxy when it responds with code 407 and asks for authentication. UseProxy is on by default anyway; it is in the example only for clarity. If you use HttpClientHandler, the code is the same: it also has the Proxy and UseProxy properties.

Set Timeout explicitly. By default HttpClient waits 100 seconds, and with a proxy that doesn't respond because of a wrong port, your program will hang for a long time on the very first request.

Step 3. One HttpClient per proxy, not per request

The most common mistake in proxy code is creating a new HttpClient in a loop. Microsoft's guidelines for HttpClient say instances should be reused for as many requests as possible, otherwise you run out of ports. The recommended options are a static or singleton client with PooledConnectionLifetime set, or short-lived clients from IHttpClientFactory.

The proxy is set for the handler, which means for the client as a whole, not for an individual request. The same guidelines state plainly that several proxies require several instances. A practical rule for Prokso: one client per port. If you need 20 sticky addresses, create 20 clients on ports 10000-10019 and keep them in a dictionary instead of creating them again.

PooledConnectionLifetime limits how long one connection to the proxy lives. For port 823 this is not critical: the proxy hands out a new IP on every request by itself. For a sticky port it is a safeguard: after the set time .NET will close the old connection to the proxy and open a new one instead of holding one socket for hours.

Step 4. SOCKS5 in .NET 6+

Starting with .NET 6, SocketsHttpHandler supports SOCKS4, SOCKS4a and SOCKS5. .NET determines the proxy type from the scheme in the address, so you just replace http:// with socks5:// and change the port to 824.

// SOCKS5: pass the proxy as a URI string (.NET 6+), no username - IP whitelist only
var socks = new SocketsHttpHandler
{
    Proxy = new WebProxy("socks5://gw.prokso.com:824"),
    UseProxy = true
};
var socksClient = new HttpClient(socks);

The documentation does not describe how .NET passes a SOCKS5 username and password through WebProxy.Credentials, so for SOCKS5 the simplest option is to turn on the IP whitelist: in the plan you add the external address of the server running your program (up to 5 addresses), and no username is needed. For most tasks you don't even need SOCKS5 in .NET: an HTTP proxy on port 823 opens https sites through a tunnel just as well.

DefaultProxy: HTTP_PROXY, HTTPS_PROXY and ALL_PROXY

If you don't give the client a proxy, .NET uses the default one from HttpClient.DefaultProxy. It reads the HTTP_PROXY, HTTPS_PROXY, ALL_PROXY and NO_PROXY variables, checking the lowercase versions first. If there are no variables, Windows uses the user's proxy settings, macOS uses the system ones, and on Linux requests go direct.

# variables are read when the process starts
export HTTPS_PROXY="http://USERNAME:PASSWORD@gw.prokso.com:823"
export ALL_PROXY="socks5://USERNAME:PASSWORD@gw.prokso.com:824"
export NO_PROXY="localhost,127.0.0.1"
dotnet run

Unlike WebProxy, in a variable you can put the username and password right in the address, and for socks5:// too - this is documented for DefaultProxy. ALL_PROXY only applies when HTTP_PROXY or HTTPS_PROXY are not set. In NO_PROXY addresses are separated by commas; .NET does not support wildcards.

This method is handy for third-party .NET tools whose code you don't touch. In your own code an explicit WebProxy is more reliable: a stray variable in the server environment won't override it.

Which exceptions to catch

.NET reports network and proxy errors as exceptions, and the exception type shows at which stage something went wrong.

try
{
    var ip = await client.GetStringAsync("https://api.ipify.org");
}
catch (TaskCanceledException e) when (e.InnerException is TimeoutException)
{
    Console.WriteLine("timeout: no response within Timeout");
}
catch (HttpRequestException e) when (e.HttpRequestError == HttpRequestError.ProxyTunnelError)
{
    Console.WriteLine("problem with the proxy tunnel: " + e.Message);   // .NET 8+
}
catch (HttpRequestException e)
{
    Console.WriteLine("another network error: " + e.Message);
}
  • TaskCanceledException with a TimeoutException inside - the Timeout has expired. With a proxy this is usually the wrong port or protocol: for example, an http:// address on port 824. See proxy timeout.
  • HttpRequestException with HttpRequestError.ProxyTunnelError (.NET 8+) - the tunnel through the proxy could not be established. The cause is most often the username, password or used-up traffic; what the proxy responds in that case is described on the code 407 page.
  • HttpRequestException after EnsureSuccessStatusCode() - the proxy worked, but the site responded with an error. The code is in the StatusCode property, for example 403 or 429 from the site itself.
  • Certificate errors - the site's certificate comes through the proxy tunnel unchanged, so look for the cause nearby: the system clock, an antivirus or a corporate filter (more details).

Checking: what the example should print

Run the program from step 2. The console should show an IP address, and it should not be your home or server IP. Run it again: on port 823 the address will be different; on a sticky port it will be the same.

The What is my IP page proves nothing here: the browser takes its own route and knows nothing about your program's proxy. But you can check the proxy string itself before writing code with the proxy checker - that way you separate a mistake in the data from a mistake in the code.

If the proxy does not work

FAQ

Which .NET version added SOCKS5 support to HttpClient?

.NET 6: that is when SOCKS4, SOCKS4a and SOCKS5 were added to SocketsHttpHandler. On older versions use HTTP port 823 or a sticky port as an HTTP proxy.

Can I change the proxy for a single request?

No, the proxy belongs to the handler, not the request. A different address needs a different HttpClient instance with its own handler - create one client per port you need and reuse them.

How many parallel requests can I make?

Up to 1000 concurrent connections per plan. How many connections .NET opens to one proxy is limited by MaxConnectionsPerServer in SocketsHttpHandler; set it no higher than the plan limit, accounting for other programs.

Will HttpClient pick up the Windows system proxy?

Yes, if you haven't set a proxy explicitly and haven't set environment variables: then DefaultProxy uses the Windows user's proxy settings. On Linux without variables, requests go direct.

Why does every request on port 823 come from a new IP, even with one client?

That's how the port works: the proxy hands out a new address on every request no matter how many clients your program has. For a session with one address, use a port from the 10000-20000 range.

Was this guide helpful?

Proxy not working? Check it in 6 steps

Install the Prokso app