簡體   English   中英

如何連接到HTTPS代理?

[英]How to connect to HTTPS proxy?

我正在嘗試使用套接字通過代理連接到HTTPS服務器。 據我所知,在使用HTTP代理時,應該將套接字連接到它,然后與它進行交互,因為它是真正的服務器。 使用HTTP這種方法可行,但HTTPS不是。 為什么?

這是連接到HTTPS服務器的簡單程序

using System;
using System.Text;
using System.Net.Sockets;
using System.Net.Security;

namespace SslTcpClient
{
    public class SslTcpClient
    {
        public static void Main(string[] args)
        {
            string host = "encrypted.google.com";
            string proxy = "127.0.0.1";//host;
            int proxyPort = 8888;//443;

            // Connect socket
            TcpClient client = new TcpClient(proxy, proxyPort);

            // Wrap in SSL stream
            SslStream sslStream = new SslStream(client.GetStream());
            sslStream.AuthenticateAsClient(host);

            // Send request
            byte[] request = Encoding.UTF8.GetBytes(String.Format("GET https://{0}/  HTTP/1.1\r\nHost: {0}\r\n\r\n", host));
            sslStream.Write(request);
            sslStream.Flush();

            // Read response
            byte[] buffer = new byte[2048];
            int bytes;
            do
            {
                bytes = sslStream.Read(buffer, 0, buffer.Length);
                Console.Write(Encoding.UTF8.GetString(buffer, 0, bytes));
            } while (bytes != 0);

            client.Close();
            Console.ReadKey();
        }
    }
}

它在proxy = hostproxyPort = 443時成功連接。 但是當我將它們設置為127.0.0.1:8888(localhost上的fiddler代理)時,它不起作用。 程序掛起在sslStream.AuthenticateAsClient(host); 為什么? Fiddler支持HTTPS(瀏覽器可以通過它連接)。

PS不,我不能在我的情況下使用HttpWebRequest

管理自己解決它。 這是解決方案:

using System;
using System.Text;
using System.Net.Sockets;
using System.Net.Security;

namespace SslTcpClient
{
    public class SslTcpClient
    {
        public static void Main(string[] args)
        {
            string host = "encrypted.google.com";
            string proxy = "127.0.0.1";//host;
            int proxyPort = 8888;//443;

            byte[] buffer = new byte[2048];
            int bytes;

            // Connect socket
            TcpClient client = new TcpClient(proxy, proxyPort);
            NetworkStream stream = client.GetStream();

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

            // Read response to CONNECT request
            // There should be loop that reads multiple packets
            bytes = stream.Read(buffer, 0, buffer.Length);
            Console.Write(Encoding.UTF8.GetString(buffer, 0, bytes));

            // Wrap in SSL stream
            SslStream sslStream = new SslStream(stream);
            sslStream.AuthenticateAsClient(host);

            // Send request
            byte[] request = Encoding.UTF8.GetBytes(String.Format("GET https://{0}/  HTTP/1.1\r\nHost: {0}\r\n\r\n", host));
            sslStream.Write(request, 0, request.Length);
            sslStream.Flush();

            // Read response
            do
            {
                bytes = sslStream.Read(buffer, 0, buffer.Length);
                Console.Write(Encoding.UTF8.GetString(buffer, 0, bytes));
            } while (bytes != 0);

            client.Close();
            Console.ReadKey();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM