C#使用Socks5代理 浏览网页
求,C#使用Socks5代理 浏览网页的方法,在百度找了好久,没有满意的答案.
C# 使用http代理 浏览网页,用起来很方便.但Socks5代理 就难找啊.
还有,一般的 Socks5 连接时,都要有帐号密码的.
这段程序仅做测试
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 && !RequireAuthorize)
{
throw new Exception( "代理服务器需要进行身份确认。 ");
}
else if (bySock5Receive[1] == 2)
{
//构造Socks5代理服务器第一连接头(无用户名密码)
byte[] bUser = Encoding.Default.GetBytes(Username);
byte[] bPass = Encoding.Default.GetBytes(Password);
int dl = 3 + bUser.Length + bPass.Length;
bySock5Send = new Byte[dl];
bySock5Send[0] = 5;
bySock5Send[1] = (byte)bUser.Length;
Array.Copy(bUser, 0, bySock5Send, 2, bUser.Length);
bySock5Send[2 + bUser.Length] = (byte)bPass.Length;
Array.Copy(bPass, 0, bySock5Send, 3 + bUser.Length, bPass.Length);
//发送Socks5代理第一次连接信息
sProxyServer.Send(bySock5Send, dl, SocketFlags.None);
bySock5Receive = new byte[100];
iRecCount = sProxyServer.Receive(bySock5Receive, bySock5Receive.Length, SocketFlags.None);
}
if (iRecCount < 2)
{
sProxyServer.Close();
throw new Exception( "不能获得代理服务器正确响应。 ");
}
if (bySock5Receive[0] != 5 || (bySock5Receive[1] != 0 && bySock5Receive[1] != 2))
{
sProxyServer.Close();
throw new Exception( "代理服务其返回的响应错误。 ");
}
if (bySock5Receive[1] == 0)
{
bySock5Send[0] = 5;
bySock5Send[1] = 1;
bySock5Send[2] = 0;
bySock5Send[3] = 1;
IPAddress ipAdd = Dns.Resolve(strRemoteHost).AddressList[0];
string strIp = ipAdd.ToString();
string[] strAryTemp = strIp.Split(new char[] { '. ' });
bySock5Send[4] = Convert.ToByte(strAryTemp[0]);
bySock5Send[5] = Convert.ToByte(strAryTemp[1]);
bySock5Send[6] = Convert.ToByte(strAryTemp[2]);
bySock5Send[7] = Convert.ToByte(strAryTemp[3]);
bySock5Send[8] = (byte)(iRemotePort / 256);
bySock5Send[9] = (byte)(iRemotePort % 256);
sProxyServer.Send(bySock5Send, bySock5Send.Length, SocketFlags.None);
iRecCount = sProxyServer.Receive(bySock5Receive, bySock5Receive.Length, SocketFlags.None);
if (bySock5Receive[0] != 5 || bySock5Receive[1] != 0)
{
sProxyServer.Close();
throw new Exception( "第二次连接Socks5代理返回数据出错。 ");
}
return true;
}
else
{
if (bySock5Receive[1] == 2)
throw new Exception( "代理服务器需要进行身份确认。 ");
else
return false;
}
}
public Socket GetSocket(string strIpAdd, int iPort)
{
try
{
IPAddress hostadd = IPAddress.Parse(strIpAdd);//Dns.Resolve(strIpAdd).AddressList[0];
IPEndPoint EPhost = new IPEndPoint(hostadd, iPort);
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Connect(EPhost);
return s;
}
catch(Exception e)
{
throw(e);
}
}
网友评论