日期:2014-05-19  浏览次数:21657 次

C#使用Socks5代理 浏览网页
求,C#使用Socks5代理   浏览网页的方法,在百度找了好久,没有满意的答案.
C#   使用http代理   浏览网页,用起来很方便.但Socks5代理   就难找啊.
还有,一般的   Socks5   连接时,都要有帐号密码的.

------解决方案--------------------
顶一下
------解决方案--------------------
直到今天才刚有兴致来尝试完成这个程序-_-b
不知楼主还需不需要

这段程序仅做测试
private void button1_Click(object sender, System.EventArgs e)
{
string proxyHost = "127.0.0.1 ";//代理的ip和端口
int proxyProt = 9050;

byte[] b;
int rl = 0;
string sr;

textBox1.Text= "new SockProxy(); ";
Application.DoEvents();
SockProxy p = new SockProxy();

// {//如果不需要密码验证,略去这段代码
// p.RequireAuthorize = true;
// p.Username = "jerry ";
// p.Password = "456321 ";
// }
textBox1.Text= "p.GetSocket(proxyHost, proxyProt); ";
Application.DoEvents();
Socket sRH=null;
try
{
sRH = p.GetSocket(proxyHost, proxyProt);

textBox1.Text= "p.ConnectProxyServer ";
Application.DoEvents();
p.ConnectProxyServer( "www.ip138.com ", 80, sRH);
textBox1.Text= "sRH.Send(b); ";
Application.DoEvents();
SSend( "GET / HTTP/1.1\r\n ",sRH);
SSend( "Connection:close\r\n ",sRH);
SSend( "Host:www.ip138.com\r\n ",sRH);
SSend( "User-agent:Mozilla/4.0\r\n ",sRH);
SSend( "Accept-language:zh-cn\r\n ",sRH);
SSend( "\r\n ",sRH);
b = new byte[1024];
textBox1.Text= "sRH.Receive(b); ";
Application.DoEvents();
rl = sRH.Receive(b);
sr = Encoding.Default.GetString(b, 0, rl);
textBox1.Text=sr;
while(sr.Length> 0)
{
Application.DoEvents();
rl = sRH.Receive(b);
sr = Encoding.Default.GetString(b, 0, rl);
textBox1.Text+=sr;
}
}
catch(Exception ex)
{
textBox1.Text=ex.ToString();
return;
}
}
private void SSend(string str,Socket sRH)
{
byte[] b= Encoding.Default.GetBytes(str);
sRH.Send(b);
}

/// <summary>
/// 使用Socks5代理服务器连接网络
/// </summary>
class SockProxy
{
bool m_RequireAuthorize = false;
string m_user = string.Empty;
string m_pass = string.Empty;

/// <summary>
/// default is false
/// </summary>
public bool RequireAuthorize
{
get { return m_RequireAuthorize; }
set { m_RequireAuthorize = value; }
}
public string Username
{
get { return m_pass; }
set { m_pass = value; }
}
public string Password
{
get { return m_user; }
set { m_user = value; }
}

public bool ConnectProxyServer(string strRemoteHost, int iRemotePort, Socket sProxyServer)
{
//构造Socks5代理服务器第一连接头(无用户名密码)
byte[] bySock5Send = new Byte[10];
bySock5Send[0] = 5;
bySock5Send[1] = 1;
bySock5Send[2] = RequireAuthorize ? (byte)2 : (byte)0;

//发送Socks5代理第一次连接信息
sProxyServer.Send(bySock5Send, 3, SocketFlags.None);

byte[] bySock5Receive = new byte[10];
int iRecCount = sProxyServer.Receive(bySock5Receive, bySock5Receive.Length, SocketFlags.None);


//用户验证
if (bySock5Receive[1] == 2 &&