C#-使用Socks5代理 瀏覽網頁,該怎麼處理

weixin_34162695發表於2018-05-11

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); 
} 
}

相關文章