代理/Socks C# 问题
如何将 proxy/socks4/socks5 添加到 C# 套接字。
我需要使用它抛出套接字。 我不想使用 WebRequest 和任何类。
private static Socket ConnectSocket(string server, int port)
{
Socket s = null;
IPHostEntry hostEntry = null;
// Get host related information.
hostEntry = Dns.GetHostEntry(server);
// Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
// an exception that occurs when the host IP Address is not compatible with the address family
// (typical in the IPv6 case).
foreach (IPAddress address in hostEntry.AddressList)
{
IPEndPoint ipe = new IPEndPoint(address, port);
Socket tempSocket =
new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
tempSocket.Connect(ipe);
if (tempSocket.Connected)
{
s = tempSocket;
break;
}
else
{
continue;
}
}
return s;
}
public static string SocketQuery(string Url, int Port, string Method = "GET", string Cookie = "", string DataFields = "")
{
string host = ExtractDomainAndPathFromURL(Url);
string request = Method.ToUpper() + " " + ExtractDomainAndPathFromURL(Url, 2) + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
((Cookie != String.Empty) ? "Cookie: " + Cookie + "\r\n" : "") +
((Method.ToUpper() == "POST") ? "Content-Length:" + DataFields.Length + "\r\n" : "") +
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13\r\n" +
"Connection: Close\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"\r\n" +
((Method.ToUpper() == "POST") ? DataFields : "");
Byte[] bytesSent = Encoding.ASCII.GetBytes(request);
Byte[] bytesReceived = new Byte[256];
Socket s = ConnectSocket(host, Port);
if (s == null)
return ("Connection failed");
s.Send(bytesSent, bytesSent.Length, 0);
int bytes = 0;
string page = String.Empty;
do
{
bytes = s.Receive(bytesReceived, bytesReceived.Length, 0);
page = page + Encoding.GetEncoding("UTF-8").GetString(bytesReceived, 0, bytes);
}
while (bytes > 0);
return page;
}
我要在这段代码中添加什么?
How I can add proxy/socks4/socks5 to C# Socket.
I need use it throw Socket.
I don't want use WebRequest and any classes.
private static Socket ConnectSocket(string server, int port)
{
Socket s = null;
IPHostEntry hostEntry = null;
// Get host related information.
hostEntry = Dns.GetHostEntry(server);
// Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
// an exception that occurs when the host IP Address is not compatible with the address family
// (typical in the IPv6 case).
foreach (IPAddress address in hostEntry.AddressList)
{
IPEndPoint ipe = new IPEndPoint(address, port);
Socket tempSocket =
new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
tempSocket.Connect(ipe);
if (tempSocket.Connected)
{
s = tempSocket;
break;
}
else
{
continue;
}
}
return s;
}
public static string SocketQuery(string Url, int Port, string Method = "GET", string Cookie = "", string DataFields = "")
{
string host = ExtractDomainAndPathFromURL(Url);
string request = Method.ToUpper() + " " + ExtractDomainAndPathFromURL(Url, 2) + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
((Cookie != String.Empty) ? "Cookie: " + Cookie + "\r\n" : "") +
((Method.ToUpper() == "POST") ? "Content-Length:" + DataFields.Length + "\r\n" : "") +
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13\r\n" +
"Connection: Close\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"\r\n" +
((Method.ToUpper() == "POST") ? DataFields : "");
Byte[] bytesSent = Encoding.ASCII.GetBytes(request);
Byte[] bytesReceived = new Byte[256];
Socket s = ConnectSocket(host, Port);
if (s == null)
return ("Connection failed");
s.Send(bytesSent, bytesSent.Length, 0);
int bytes = 0;
string page = String.Empty;
do
{
bytes = s.Receive(bytesReceived, bytesReceived.Length, 0);
page = page + Encoding.GetEncoding("UTF-8").GetString(bytesReceived, 0, bytes);
}
while (bytes > 0);
return page;
}
What will I add to this code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不太清楚为什么你说你不想使用
WebRequest
(或者我想,WebClient
就此事而言),当你明确创建一个 http web 请求时,但我我会假设你有你的理由!简而言之,.Net 中没有支持 SOCKS 代理的内置方法,并且不支持低至套接字级别的 http 代理(这么低的级别实际上没有意义,因为不能保证请求是正确的) http 请求)。 .Net 在较高的
HttpWebRequest
/WebClient
层内置了 http 代理支持 - 但您已经忽略了这一点。我认为你的选择是:
(
HttpWebRequest
或WebClient
)并获取免费的 http 代理支持。
Not quite sure why you say you don't want to use
WebRequest
(or I imagine,WebClient
for that matter) when you are clearly creating an http web request, but I'll assume you have your reasons!In short, there is no built in way of supporting SOCKS proxies in .Net, and there is not support for http proxies at the level as low as sockets (it wouldn't really make sense this low as there is no guarantee the requests are http requests). There is http proxy support built into .Net at the higher
HttpWebRequest
/WebClient
layer - but you've already discounted this.I think your options are:
(
HttpWebRequest
orWebClient
) and gethttp proxy support for free.
不要打开到实际位置的套接字,而是尝试打开到代理的套接字。
instead of opening a socket to the actual location, try to open the socket to the proxy.