0

每当我尝试对 google.com 执行 GET 请求时,都会收到此错误:

Get "https://www.google.com/": net/http: HTTP/1.x transport connection broken: malformed HTTP response

我想尝试获取 google 的主页,但我不知道为什么会这样。有人请帮忙。我正在使用 golang 的 uTls 这是我的代码:

func tls_connection(URL string) *http.Response {

    //Creating a cookie jar
    cookieJar, _ := cookiejar.New(nil)

    // Creating the client wrapper
    client := &http.Client{
        Jar: cookieJar,
        Transport: &http.Transport{
            DialTLSContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
                tcpConn, err := (&net.Dialer{}).DialContext(ctx, network, addr)
                if err != nil {
                    return nil, err
                }
                config := tls.Config{InsecureSkipVerify: true}
                tlsConn := tls.UClient(tcpConn, &config, tls.HelloChrome_Auto)

                // Parsing the URL for SNI
                u, _ := url.Parse(URL)
                tlsConn.SetSNI(u.Host)

                err = tlsConn.Handshake()
                if err != nil {
                    return nil, fmt.Errorf("uTlsConn.Handshake() error: %w", err)
                }
                return tlsConn, nil
            },
        },
    }

    // Establishes a request
    req, err := http.NewRequest("GET", URL, nil)
    req.Header.Add("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")

    // the client.do wraps the request
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println(err)
    } else {
        fmt.Println(resp.StatusCode)
        body, _ := ioutil.ReadAll(resp.Body)
        fmt.Println(string(body))
    }
    return resp
}
4

0 回答 0