注册
登录
 文档中心 产品介绍 开发指南 API接口 代码样例 使用帮助
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());
			}
		}
	}
}


立即注册站大爷用户,免费试用全部产品
立即注册站大爷用户,免费试用全部产品