如何连接到HTTPS代理? [英] How to connect to HTTPS proxy?

查看:747
本文介绍了如何连接到HTTPS代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过使用代理套接字连接到HTTPS服务器。据我所知,使用HTTP代理应该连接插座,然后与它进行交互,因为它是真正的服务器时。通过HTTP这种方法的工作原理,但与HTTPS是不是。为什么呢?

I'm trying to connect to HTTPS server through proxy using sockets. As far as I know when using HTTP proxy one should connect socket to it and then interact with it as it is the real server. With HTTP this approach works, but with HTTPS isn't. Why?

下面是一个连接到HTTPS服务器简单的程序

Here's simple program that connects to HTTPS server

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

连接成功时,代理=主机 proxyPort = 443 。但是,当我将它们设置为127.0.0.1:8888(本地主机上的提琴手代理),这是行不通的。项目挂在 sslStream.AuthenticateAsClient(主机); 为什么呢?提琴手支持HTTPS(浏览器都可以通过它来连接)。

It successfully connects when proxy = host and proxyPort = 443. But when I set them to 127.0.0.1:8888 (fiddler proxy on localhost) it doesn't work. Program hangs at sslStream.AuthenticateAsClient(host); Why? Fiddler supports HTTPS (browsers are able to connect through it).

P.S。不,我不能使用的HttpWebRequest 在我的情况。

P.S. No, I can't use HttpWebRequest in my case.

推荐答案

设法解决它自己。这里的解决方案:

Managed to solve it myself. Here's the solution:

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

这篇关于如何连接到HTTPS代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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