C# Tunnel Proxy: HttpClient and HttpWebRequest

C# HttpClient and HttpWebRequest integrating 16Yun Crawler Proxy.

16Yun Engineering TeamMay 3, 20261 min read

HttpClient

C# 的 HttpClient 通过 WebProxy 类配置代理:

using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        var host = Environment.GetEnvironmentVariable("PROXY_HOST") ?? "t.16yun.cn";
        var port = Environment.GetEnvironmentVariable("PROXY_PORT") ?? "31111";
        var user = Environment.GetEnvironmentVariable("PROXY_USERNAME") ?? "user";
        var pass = Environment.GetEnvironmentVariable("PROXY_PASSWORD") ?? "password";

        var proxy = new WebProxy($"{host}:{port}")
        {
            Credentials = new NetworkCredential(user, pass)
        };

        var handler = new HttpClientHandler
        {
            Proxy = proxy,
            UseProxy = true,
            ServerCertificateCustomValidationCallback = (_, _, _, _) => true
        };

        using var client = new HttpClient(handler);
        client.Timeout = TimeSpan.FromSeconds(15);

        var response = await client.GetStringAsync("https://httpbin.org/ip");
        Console.WriteLine(response);
    }
}

HttpWebRequest

var host = Environment.GetEnvironmentVariable("PROXY_HOST") ?? "t.16yun.cn";
var port = Environment.GetEnvironmentVariable("PROXY_PORT") ?? "31111";
var user = Environment.GetEnvironmentVariable("PROXY_USERNAME") ?? "user";
var pass = Environment.GetEnvironmentVariable("PROXY_PASSWORD") ?? "password";

var proxy = new WebProxy($"{host}:{port}")
{
    Credentials = new NetworkCredential(user, pass)
};

var request = (HttpWebRequest)WebRequest.Create("https://httpbin.org/ip");
request.Proxy = proxy;
request.Timeout = 15000;

using var response = (HttpWebResponse)request.GetResponse();
using var reader = new System.IO.StreamReader(response.GetResponseStream());
Console.WriteLine(reader.ReadToEnd());

四种场景对比

场景HttpClientHttpWebRequest
A:强制切换每次新建 HttpClient每次新建 HttpWebRequest
B:保持 IP复用 HttpClient 实例复用 ServicePoint
C-HTTP:Proxy-TunnelWebProxy + 自定义头request.Headers["Proxy-Tunnel"]
C-HTTPS:Proxy-Tunnel需自定义 ICertificatePolicy需自定义认证策略

推荐使用 HttpClient(现代、异步、社区标准)。HttpWebRequest 是旧式 API,保持兼容而存在。

Need an enterprise proxy plan?

We can tailor architecture to your target domains, concurrency, and reliability goals.