C# and .NET

Route .NET HttpClient through NyxonProxies with WebProxy and NetworkCredential: HTTPS, SOCKS5, rotation via pooled connection lifetime, sticky sessions, errors.

.NET's HttpClient takes a WebProxy with a NetworkCredential, and it supports both the HTTP and the SOCKS5 port. The password goes in as it is, so special characters need no encoding.

Basic request

c#
using System.Net;

var proxy = new WebProxy("http://geo.gw.nyxon.sh:8080")
{
    Credentials = new NetworkCredential("USERNAME", "PASSWORD"),
};
var handler = new HttpClientHandler { Proxy = proxy, UseProxy = true };
using var client = new HttpClient(handler) { Timeout = TimeSpan.FromSeconds(30) };

Console.WriteLine(await client.GetStringAsync("https://api.ipify.org"));

The proxy address starts with http:// even for HTTPS sites. HttpClient opens a tunnel through the gateway and runs TLS to the site inside it. Create the client once and reuse it.

A new IP on every request

HttpClient keeps connections in a pool and reuses them, so requests to one site share an IP. With PooledConnectionLifetime set to zero, every request opens a new connection, which gets a new IP on a rotating username:

c#
using System.Net;

var handler = new SocketsHttpHandler
{
    Proxy = new WebProxy("http://geo.gw.nyxon.sh:8080")
    {
        Credentials = new NetworkCredential("USERNAME", "PASSWORD"),
    },
    UseProxy = true,
    PooledConnectionLifetime = TimeSpan.Zero,
};
using var client = new HttpClient(handler) { Timeout = TimeSpan.FromSeconds(30) };

for (var i = 0; i < 3; i++)
    Console.WriteLine(await client.GetStringAsync("https://api.ipify.org"));

Country and sticky session

Targeting and sessions go in the username:

c#
using System.Net;

var sid = Guid.NewGuid().ToString("N")[..8];
var user = $"USERNAME-country-de-session-{sid}-time-10";

var proxy = new WebProxy("http://geo.gw.nyxon.sh:8080")
{
    Credentials = new NetworkCredential(user, "PASSWORD"),
};
using var client = new HttpClient(new HttpClientHandler { Proxy = proxy, UseProxy = true });

for (var i = 0; i < 2; i++)
    Console.WriteLine(await client.GetStringAsync("https://api.ipify.org"));

Both requests print the same German IP for up to 10 minutes. See Rotating and sticky sessions.

SOCKS5

Since .NET 6, WebProxy accepts socks5:// addresses, with the same credentials:

c#
using System.Net;

var proxy = new WebProxy("socks5://geo.gw.nyxon.sh:1080")
{
    Credentials = new NetworkCredential("USERNAME", "PASSWORD"),
};
using var client = new HttpClient(new HttpClientHandler { Proxy = proxy, UseProxy = true });

Console.WriteLine(await client.GetStringAsync("https://api.ipify.org"));

.NET sends the hostname to the gateway, so DNS is resolved on the proxy side.

Errors

ErrorCause
HttpRequestException mentioning status code 407Wrong username or password.
HttpRequestException with a connection errorWrong host or port, or a firewall.
TaskCanceledExceptionThe request took longer than the timeout.

More in Error codes and Troubleshooting.

Last updated

Still stuck?

People answer the live chat around the clock. Send the proxy line you used (without the password) and the error you see.