使用CCProxy的TcpClient与WebClient

时间:2015-05-14 21:37:19

标签: c# proxy tcpclient

我有这段代码:

        using (TcpClient tc = new TcpClient())
        {
            button1.Enabled = false;
            //string Data = System.Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("user:123"));
            //tc.Connect("bot.whatismyipaddress.com", 80);
            //tc.Connect("xxx.xx.xxx.xxx", 3128); external proxy works
            try
            {
                //tc.Connect("192.168.1.2", 808); internal proxy doesn't work
                using (NetworkStream ns = tc.GetStream())
                {
                    using (StreamWriter sw = new StreamWriter(ns))
                    {
                        using (StreamReader sr = new StreamReader(ns))
                        {
                            sw.Write(@"GET / HTTP/1.1
Host: bot.whatismyipaddress.com
Connection: close
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36 OPR/28.0.1750.40
DNT: 1
Accept-Encoding: gzip, deflate, lzma, sdch
Accept-Language: en-US,en;q=0.8

");
                            sw.Flush();
                            string Res = sr.ReadToEnd();
                            richTextBox1.Text = Res;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                richTextBox1.Text = ex.Message;
            }
            button1.Enabled = true;
        }
    }

哪个外部代理URL工作得很好,但是我想尝试认证部分,所以我使用了CCProxy,然后我意识到代码不适用于本地CCProxy(虽然WebClient运行良好),TcpClient响应完全是空的。

我不知道它如何在外部工作而且不在内部! [编辑] 有没有办法测试ccproxy以外的身份验证?

1 个答案:

答案 0 :(得分:1)

好的这是答案

byte[] buffer = new byte[2048];
        int bytes;
        using (TcpClient tc = new TcpClient("192.168.1.2", 808))
        {                
            button1.Enabled = false;                
            NetworkStream stream = tc.GetStream();

            // Establish Tcp tunnel
            byte[] tunnelRequest = Encoding.UTF8.GetBytes(String.Format("CONNECT {0}:80  HTTP/1.1\r\nHost: {0}\r\n\r\n", "bot.whatismyipaddress.com"));
            stream.Write(tunnelRequest, 0, tunnelRequest.Length);
            stream.Flush();

            // Read response to CONNECT request
            bytes = stream.Read(buffer, 0, buffer.Length);
            richTextBox1.Text  = Encoding.UTF8.GetString(buffer, 0, bytes);
            // Establish Get
            tunnelRequest = Encoding.UTF8.GetBytes(String.Format(@"GET / HTTP/1.1
Host: bot.whatismyipaddress.com
Connection: close
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36 OPR/28.0.1750.40
DNT: 1
Accept-Encoding: gzip, deflate, lzma, sdch
Accept-Language: en-US,en;q=0.8

"));
            stream.Write(tunnelRequest, 0, tunnelRequest.Length);
            stream.Flush();
            bytes = stream.Read(buffer, 0, buffer.Length);
            richTextBox1.Text += Encoding.UTF8.GetString(buffer, 0, bytes);
            button1.Enabled = true;

我必须' CONNECT'代理首先通过它打开连接然后我发送' GET'照常。 并实现用户并传递(基本授权)

string Data = System.Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("user:123"));
byte[] tunnelRequest = Encoding.UTF8.GetBytes(String.Format("CONNECT {0}:80  HTTP/1.1\r\nHost: {0}\r\nProxy-Authorization: Basic {1}\r\n\r\n", "bot.whatismyipaddress.com", Data));