You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何所有网站均拒绝我的CONNECT请求?搭建正向代理遇阻

问题原因与解决方案

核心错误:CONNECT请求发错了对象

CONNECT方法的作用是向HTTP代理服务器请求建立TCP隧道,用于转发后续的HTTPS流量。你现在直接把CONNECT请求发给了目标网站(reqbin、Google等),但这些网站本身不是代理服务器,它们的Web服务(或Cloudflare这类CDN)不会处理CONNECT请求,所以返回400错误或"method not supported"是完全正常的。

现象背后的原因

  • Cloudflare返回"HTTP CONNECT is not supported":Cloudflare作为CDN,仅处理常规HTTP/HTTPS请求(GET、POST等),不响应CONNECT方法。
  • Google返回400:Google的Web服务器不接受CONNECT请求,判定为格式错误的请求。
  • httpbin提示"method not supported":httpbin仅模拟常规HTTP方法的响应,未实现CONNECT的处理逻辑。

修正方案:将CONNECT请求发给代理服务器

要测试CONNECT功能,你需要先搭建一个正向代理服务器,再将CONNECT请求发送给这个代理,而非目标网站。

第一步:搭建简单正向代理服务器

import socket
import threading

def handle_client(client_socket):
    # 解析CONNECT请求
    request = client_socket.recv(1024).decode()
    if not request.startswith("CONNECT"):
        client_socket.close()
        return
    
    # 提取目标主机和端口
    target_info = request.split()[1]
    target_host, target_port = target_info.split(":")
    target_port = int(target_port)
    
    # 连接目标服务器
    try:
        target_socket = socket.create_connection((target_host, target_port))
        # 返回200响应表示隧道建立成功
        client_socket.send(b"HTTP/1.1 200 Connection Established\r\n\r\n")
        
        # 双向转发数据
        def forward(source, destination):
            while True:
                data = source.recv(4096)
                if not data:
                    break
                destination.sendall(data)
        
        threading.Thread(target=forward, args=(client_socket, target_socket)).start()
        threading.Thread(target=forward, args=(target_socket, client_socket)).start()
    except Exception as e:
        client_socket.send(b"HTTP/1.1 500 Internal Server Error\r\n\r\n")
        client_socket.close()

def run_proxy(host="127.0.0.1", port=8080):
    proxy_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    proxy_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    proxy_socket.bind((host, port))
    proxy_socket.listen(5)
    print(f"Proxy running on {host}:{port}")
    
    while True:
        client_conn, client_addr = proxy_socket.accept()
        threading.Thread(target=handle_client, args=(client_conn,)).start()

if __name__ == "__main__":
    run_proxy()

第二步:修改测试代码,向代理发送CONNECT请求

import httpx

def test_proxy_connect():
    # 本地代理地址
    proxy_url = "http://127.0.0.1:8080"
    # CONNECT请求的目标站点
    target_url = "reqbin.com:443"

    with httpx.Client(proxies={"all://": proxy_url}) as client:
        # 向代理发送CONNECT请求
        response = client.request("CONNECT", target_url)
        print(response.status_code)
        if response.status_code == 200:
            print("隧道建立成功,可通过代理发送HTTPS请求")
            # 示例:通过代理访问目标站点
            https_response = client.get("https://reqbin.com")
            print(https_response.status_code, https_response.text[:500])

test_proxy_connect()

针对你的疑问的解答

  • CONNECT方法没有被禁用或废弃,它是HTTPS代理的核心机制,所有正常的HTTP代理都在使用它。
  • 不是所有HTTP代理都无法工作,只是你错误地将请求发给了目标网站而非代理服务器。

内容的提问来源于stack exchange,提问作者Hiello

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 05:46:10