【问题标题】:golang proper http2 requestgolang 正确的 http2 请求
【发布时间】:2021-08-07 02:43:11
【问题描述】:

我想在 go 中发出一个 http/2 请求,并在那里遇到了一些问题。 如何在 go 中发出正确的 http/2 请求?

错误:获取“https://webhook.site/aae1e0ab-3e48-49c8-8cd0-526e12ee4077”:http2:意外的ALPN协议“”;想要“h2”(为什么?其他网站都在工作)

代码:

t := &http2.Transport{}
    c := &http.Client{
        Transport: t,
    }


        r, err := http.NewRequest("GET", "https://webhook.site/aae1e0ab-3e48-49c8-8cd0-526e12ee4077", nil)
        if err != nil {
            fmt.Println(err)
        }
        r.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.2 Safari/605.1.15")
        r.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")

        resp, err := c.Do(r)
        if err != nil {
            fmt.Println(err)
        }
        defer resp.Body.Close()

        body, err := ioutil.ReadAll(resp.Body)
        if err != nil {
            fmt.Println(err)
        }
        fmt.Println(string(body))
    ```

【问题讨论】:

  • 那个服务器不支持http2

标签: http go http2


【解决方案1】:

如前所述,该服务器不支持 HTTP2:

PS C:\> curl.exe -I --http2-prior-knowledge https://webhook.site
curl: (16) Error in the HTTP2 framing layer

对比一下:

PS C:\> curl.exe -I --http2-prior-knowledge https://example.com
HTTP/2 200

https://curl.se/docs/manpage.html#--http2-prior-knowledge

【讨论】:

    猜你喜欢
    • 2018-11-04
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    • 1970-01-01
    • 1970-01-01
    • 2019-10-19
    • 2019-08-10
    • 1970-01-01
    相关资源
    最近更新 更多