代理/袜子C#问题 [英] Proxy/Socks C# problem

查看:190
本文介绍了代理/袜子C#问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么可以添加代理/ SOCKS4 / SOCKS5到C#的Socket。

How I can add proxy/socks4/socks5 to C# Socket.

我需要用它扔插槽。 我不想使用WebRequest类和任何类。

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

我会添加到这个code?

What will I add to this code?

推荐答案

不明白为什么你说你不想使用的WebRequest (或者我想象, Web客户端对于这个问题),当你清楚创建HTTP Web请求,但我会假设你有你的理由!

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!

总之,没有内置的支持SOCKS代理。NET中的方式,并没有为HTTP代理的级别低插槽支持(它不会真正弄懂这个低难保该请求是HTTP请求)。有一个在较高的HttpWebRequest / Web客户端层内置于净HTTP代理支持 - 但你已经贴现这

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.

我觉得你的选择是:

  • 使用正确的工具来完成工作 (的HttpWebRequest Web客户端),并得到 免费的http代理的支持。
  • 使用第三方实施的SOCKS支持,其中有出现,如果你做一个谷歌搜索是少数。 (例如的一个)。
  • Use the correct tool for the job (HttpWebRequest or WebClient) and get http proxy support for free.
  • Use a third party implementation of SOCKS support, of which there appear to be a few if you do a Google search. (for example this one).

这篇关于代理/袜子C#问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆