在C#网络编程中使用匿名Socks5代理

简介:

 我们知道编程的时候使用代理服务器并不是一件爽心悦目的事情,可是很多时候又是迫不得已的emsad.gif。微软在.NET Framework得System.Net名称空间里给我们提供了一个WebProxy类,不过这是一个http代理,这种代理使用起来受限太多,很不方便。如果我们需要访问更多的网络服务,socks代理是一个理想的选择。下面是C#编写的一个匿名socks5代理使用示例:

ExpandedBlockStart.gif #region 连接匿名socks5服务器代码
InBlock.gifprivate bool ConnectProxyServer(string strRemoteHost, int iRemotePort, Socket sProxyServer)
ExpandedSubBlockStart.gif{
InBlock.gif    //构造Socks5代理服务器第一连接头(无用户名密码)
InBlock.gif
    byte[] bySock5Send = new Byte[10];
InBlock.gif    bySock5Send[0] = 5;
InBlock.gif    bySock5Send[1] = 1;
InBlock.gif    bySock5Send[2] = 0;
InBlock.gif
InBlock.gif    //发送Socks5代理第一次连接信息
InBlock.gif
    sProxyServer.Send(bySock5Send, 3, SocketFlags.None);
InBlock.gif
InBlock.gif    byte[] bySock5Receive = new byte[10];
InBlock.gif    int iRecCount = sProxyServer.Receive(bySock5Receive, bySock5Receive.Length, SocketFlags.None);
InBlock.gif
InBlock.gif    if ( iRecCount<2 ) 
ExpandedSubBlockStart.gif    {
InBlock.gif        sProxyServer.Close();
InBlock.gif        throw new Exception("不能获得代理服务器正确响应。");
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    if ( bySock5Receive[0]!=5 || ( bySock5Receive[1]!=0 && bySock5Receive[1]!=2 ) )
ExpandedSubBlockStart.gif    {
InBlock.gif        sProxyServer.Close(); 
InBlock.gif        throw new Exception("代理服务其返回的响应错误。");
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    if ( bySock5Receive[1]==0 )
ExpandedSubBlockStart.gif    {
InBlock.gif        bySock5Send[0] = 5;
InBlock.gif        bySock5Send[1] = 1;
InBlock.gif        bySock5Send[2] = 0;
InBlock.gif        bySock5Send[3] = 1;
InBlock.gif
InBlock.gif        IPAddress ipAdd = Dns.Resolve(strRemoteHost).AddressList[0];
InBlock.gif        string strIp = ipAdd.ToString();
ExpandedSubBlockStart.gif        string []strAryTemp = strIp.Split(new char[] {'.'});
InBlock.gif        bySock5Send[4] = Convert.ToByte(strAryTemp[0]);
InBlock.gif        bySock5Send[5] = Convert.ToByte(strAryTemp[1]);
InBlock.gif        bySock5Send[6] = Convert.ToByte(strAryTemp[2]);
InBlock.gif        bySock5Send[7] = Convert.ToByte(strAryTemp[3]);
InBlock.gif
InBlock.gif        bySock5Send[8] = (byte)(iRemotePort/256);
InBlock.gif        bySock5Send[9] = (byte)(iRemotePort%256);
InBlock.gif
InBlock.gif        sProxyServer.Send(bySock5Send, bySock5Send.Length, SocketFlags.None);
InBlock.gif        iRecCount = sProxyServer.Receive(bySock5Receive, bySock5Receive.Length, SocketFlags.None);
InBlock.gif
InBlock.gif        if ( bySock5Receive[0]!=5 || bySock5Receive[1]!=0 )
ExpandedSubBlockStart.gif        {
InBlock.gif            sProxyServer.Close();
InBlock.gif            throw new Exception("第二次连接Socks5代理返回数据出错。");
ExpandedSubBlockEnd.gif        }

InBlock.gif        return true ;
ExpandedSubBlockEnd.gif    }

InBlock.gif    else
ExpandedSubBlockStart.gif    {
InBlock.gif        if ( bySock5Receive[1]==2 ) 
InBlock.gif            throw new Exception("代理服务器需要进行身份确认。");
InBlock.gif        else return false ;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif#endregion

   关于socks5代理的详细文档,可以参看RFC 1928

   使用上面的代码需要有一个匿名的socks5代理服务器,然后调用:

None.gif private Socket GetSocket( string strIpAdd,  int iPort)
ExpandedBlockStart.gif {
InBlock.gif    IPAddress hostadd = Dns.Resolve(strIpAdd).AddressList[0];
InBlock.gif    IPEndPoint EPhost = new IPEndPoint(hostadd, iPort);
InBlock.gif    Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
InBlock.gif    s.Connect(EPhost);
InBlock.gif    return s ;
ExpandedBlockEnd.gif}

None.gif
None.gifsRemoteHost  =  GetSocket(proxyHost, proxyProt);
None.gifConnectProxyServer("61.135.134.24", 80
, sRemoteHost);
None.gif

   如果您需要使用非匿名的socks5代理,把RFC 1928后面关于用户验证的部分实现就可以了,由于我使用的都是匿名的socks5代理,所以没有做这个部分得工作emembarrassed.gif

    

目录
相关文章
|
1月前
|
安全 网络协议 网络安全
代理IP、Socks5代理与网络安全:保护隐私与防御威胁的技术探索
代理IP、Socks5代理与网络安全:保护隐私与防御威胁的技术探索
41 0
|
6月前
|
数据采集 JSON API
C#网络爬虫实例:使用RestSharp获取Reddit首页的JSON数据并解析
C#网络爬虫实例:使用RestSharp获取Reddit首页的JSON数据并解析
|
5天前
|
运维 前端开发 Devops
云效产品使用报错问题之流水线打包docker镜像时报网络代理有问题如何解决
本合集将整理呈现用户在使用过程中遇到的报错及其对应的解决办法,包括但不限于账户权限设置错误、项目配置不正确、代码提交冲突、构建任务执行失败、测试环境异常、需求流转阻塞等问题。阿里云云效是一站式企业级研发协同和DevOps平台,为企业提供从需求规划、开发、测试、发布到运维、运营的全流程端到端服务和工具支撑,致力于提升企业的研发效能和创新能力。
|
8天前
|
缓存 安全 网络协议
代理ip会不会影响网络速度和稳定性
代理ip会不会影响网络速度和稳定性
|
1月前
|
机器学习/深度学习 安全 算法
网络代理技术:保障隐私与增强安全
网络代理技术:保障隐私与增强安全
28 0
|
2月前
|
域名解析 缓存 网络协议
|
3月前
|
数据采集 监控 调度
C#网络爬虫之TianyaCrawler实战经验分享
C#网络爬虫之TianyaCrawler实战经验分享
最新对比,芝麻代理、青果网络、快代理的代理IP哪家好?
朋友们也可以自己去测试,再来把我的赞补上。