如何修复 net/http:HTTP/1.x 传输连接中断:HTTP 响应格式错误

问题描述

每当我尝试对 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,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
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)