C#使用HttpToSocks5调用Socks5代理IP的代码样例
站大爷
官方
2024-01-20
1741 浏览
温馨提示:
1. HttpToSocks5Proxy是第三方库,请先安装后再运行。
2. http/https网页均可适用
3. 代码样例中的代理IP均为虚构,请添加通过站大爷提供的API提取链接获取IP后添加使用
using System;
using System.Net.Http;
using System.IO;
using System.IO.Compression;
using MihaZupan;
namespace httptosocks
{
class Program
{
static void Main(string[] args)
{
// 要访问的目标网页
string page_url = "https://example.com";
// 构造请求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(page_url);
request.Method = "GET";
request.Headers.Add("Accept-Encoding", "Gzip"); // 使用gzip压缩传输数据让访问更快
// 代理服务器
string proxy_ip = "168.168.168.168";
int proxy_port = 12578;
// 用户名+密码授权
string username = "myusername";
string password = "mypassword";
// 设置代理 (已绑定终端IP)
// request.Proxy = new WebProxy(168.168.168.168, 25825);
// 请求目标网页
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Console.WriteLine((int)response.StatusCode); // 获取状态码
// 解压缩读取返回内容
using (StreamReader reader = new StreamReader(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress))) {
Console.WriteLine(reader.ReadToEnd());
}
}
}
}