C# HttpClient with WebProxy support. Configure KnoxProxy in .NET applications using the built-in System.Net.Http namespace.
Configure HttpClientHandler with WebProxy.
var handler = new HttpClientHandler
{
Proxy = new WebProxy("http://gw.knoxproxy.com:7000")
{
Credentials = new NetworkCredential("USER", "PASS")
},
UseProxy = true
};Initialize HttpClient with handler.
var client = new HttpClient(handler);Send request through proxy.
var response = await client.GetStringAsync("https://httpbin.org/ip");
Console.WriteLine(response);Add country to username.
Credentials = new NetworkCredential("USER-country-us", "PASS")Run and verify exit IP.
Use IDisposable pattern.
using var client = new HttpClient(handler);using System.Net;
using System.Net.Http;
var handler = new HttpClientHandler
{
Proxy = new WebProxy("http://gw.knoxproxy.com:7000")
{
Credentials = new NetworkCredential("USER", "PASS")
},
UseProxy = true
};
using var client = new HttpClient(handler) { Timeout = TimeSpan.FromSeconds(30) };
var response = await client.GetStringAsync("https://httpbin.org/ip");
Console.WriteLine($"Exit IP: {response}");
// Country-targeted
handler.Proxy = new WebProxy("http://gw.knoxproxy.com:7000")
{
Credentials = new NetworkCredential("USER-country-de", "PASS")
};HttpClient maintains connection pools. Create new HttpClientHandler instances for IP rotation, or use -session-{id} for explicit session control.
| Problem | Fix |
|---|---|
| HttpRequestException: proxy tunnel failed | Verify NetworkCredential username and password. |
| TaskCanceledException | Increase client.Timeout. |
USER-country-de-city-berlin-session-profile07Order matters -- geo flags before the session flag. The session name is free text; use the profile ID so the mapping is self-documenting. Password stays as issued; no flags belong there. HTTP on :7000, SOCKS5 on :7001, same credentials.
HttpClient is built into .NET and is the option Microsoft recommends, configured here with an HttpClientHandler whose Proxy property points at a WebProxy wrapping the KnoxProxy gateway and NetworkCredential. RestSharp is a popular third-party alternative that also supports proxies, set through RestClientOptions.Proxy = new WebProxy(...), if you prefer its higher-level request API.
Register a named HttpClient through IHttpClientFactory in your ASP.NET startup code, and configure its HttpClientHandler with the same WebProxy and NetworkCredential pattern shown for a plain console app. IHttpClientFactory then manages pooling and reuse for you, which avoids the socket exhaustion problems that come from creating a new HttpClient per request.
Yes, starting in .NET 6. Create a WebProxy pointed at "socks5://gw.knoxproxy.com:7001" instead of the HTTP port 7000 address, and attach the same NetworkCredential holding your KnoxProxy username and password. Assign that proxy to an HttpClientHandler exactly as you would for an HTTP proxy; the rest of the request code stays unchanged.
Socket exhaustion happens when a new HttpClient, and its underlying handler, gets created and disposed on every request instead of reused. In ASP.NET, use IHttpClientFactory to manage pooling automatically; in console apps, create one HttpClient with your KnoxProxy handler at startup and reuse it for the application lifetime instead of a using block per call.
Rotating residential proxies -- 5 minutes setup, instant activation, 14-day money-back guarantee.