Use a proxy with C# HttpClient
Complete HTTPS access using .NET HttpClient, WebProxy, proxy accounting, and request timeouts.
Maintained by the ZZHTTP engineering teamUpdated September 20, 2026
.NET Applications can be created through HttpClientHandler.Proxy Binding WebProxy. The examples decompose the proxy address and credentials for readability, facilitating credential rotation and log desensitization.
Precondition
- Caller Project Use Support
HttpClientThe current .NET runtime. - The account is closed.
us.zzhttp.com:7878; Injection through controlled environmentPROXY_USERNAMEandPROXY_PASSWORD. - Long lifecycle services through reliance on injection or client factory management connection reuse.
Complete example
using System.Net;
var proxyUsername = Environment.GetEnvironmentVariable("PROXY_USERNAME")
?? throw new InvalidOperationException("Missing PROXY_USERNAME");
var proxyPassword = Environment.GetEnvironmentVariable("PROXY_PASSWORD")
?? throw new InvalidOperationException("Missing PROXY_PASSWORD");
var proxy = new WebProxy(new Uri("http://us.zzhttp.com:7878"))
{
Credentials = new NetworkCredential(proxyUsername, proxyPassword),
};
using var handler = new HttpClientHandler
{
Proxy = proxy,
UseProxy = true,
};
using var client = new HttpClient(handler)
{
Timeout = TimeSpan.FromSeconds(30),
};
using var response = await client.GetAsync("https://ipinfo.io/json");
response.EnsureSuccessStatusCode();
Console.WriteLine(await response.Content.ReadAsStringAsync());
Expected results
- HTTPS requests are tunneled through a configured HTTP proxy.
EnsureSuccessStatusCodeThrow an exception when the target returns to an error state.- Production services reuse clients and processors, and do not create connection pools repeatedly in high-frequency requests.
Error handling
- 407 Inspection
NetworkCredentialUsername, password and current rights. TaskCanceledExceptionThe revocation token is used to determine whether the revocation is active or requested to timeout.HttpRequestExceptionThe status and internal anomalies of the system shall be recorded in a hierarchical manner, but shall not contain any evidence.
Safety tips
Don't take it. WebProxy The environment variables or complete exception requests are serialized to the log. The update of credentials should eliminate the legacy processors and connections and ensure that subsequent requests use the new identity.
Did this answer your question?
Feedback is used only to improve the public documentation and does not submit accounts or request information.