# Use a proxy with C# HttpClient

Complete HTTPS access using .NET HttpClient, WebProxy, proxy accounting, and request timeouts.

.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 `HttpClient` The current .NET runtime.
- The account is closed. `us.zzhttp.com:7878`; Injection through controlled environment `PROXY_USERNAME` and `PROXY_PASSWORD`.
- Long lifecycle services through reliance on injection or client factory management connection reuse.

## Complete example

```csharp
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.
- `EnsureSuccessStatusCode` Throw 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 `NetworkCredential` Username, password and current rights.
- `TaskCanceledException` The revocation token is used to determine whether the revocation is active or requested to timeout.
- `HttpRequestException` The 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.

html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}
