使用 CCProxy 的 TcpClient 与 WebClient

TcpClient vs WebClient with CCProxy

我有这个代码:

        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 之外,还有其他方法可以测试身份验证吗?

好的,这是答案

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