Golang适当的HTTP2请求

huangapple go评论115阅读模式
英文:

golang proper http2 request

问题

我想在Go语言中进行一个HTTP/2请求,但遇到了一些问题。如何在Go中进行正确的HTTP/2请求?

错误信息:Get "https://webhook.site/aae1e0ab-3e48-49c8-8cd0-526e12ee4077": http2: unexpected ALPN protocol ""; want "h2"(为什么?其他网站可以正常工作)

代码:

  1. t := &http2.Transport{}
  2. c := &http.Client{
  3. Transport: t,
  4. }
  5. r, err := http.NewRequest("GET", "https://webhook.site/aae1e0ab-3e48-49c8-8cd0-526e12ee4077", nil)
  6. if err != nil {
  7. fmt.Println(err)
  8. }
  9. 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")
  10. r.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
  11. resp, err := c.Do(r)
  12. if err != nil {
  13. fmt.Println(err)
  14. }
  15. defer resp.Body.Close()
  16. body, err := ioutil.ReadAll(resp.Body)
  17. if err != nil {
  18. fmt.Println(err)
  19. }
  20. fmt.Println(string(body))
英文:

I want to make an http/2 request in go and got to a few issues there.
How to make proper http/2 requests in go?

Error: Get "https://webhook.site/aae1e0ab-3e48-49c8-8cd0-526e12ee4077": http2: unexpected ALPN protocol ""; want "h2" (Why? Other sites are working)

Code:

  1. t := &http2.Transport{}
  2. c := &http.Client{
  3. Transport: t,
  4. }
  5. r, err := http.NewRequest("GET", "https://webhook.site/aae1e0ab-3e48-49c8-8cd0-526e12ee4077", nil)
  6. if err != nil {
  7. fmt.Println(err)
  8. }
  9. 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")
  10. r.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
  11. resp, err := c.Do(r)
  12. if err != nil {
  13. fmt.Println(err)
  14. }
  15. defer resp.Body.Close()
  16. body, err := ioutil.ReadAll(resp.Body)
  17. if err != nil {
  18. fmt.Println(err)
  19. }
  20. fmt.Println(string(body))
  21. ```
  22. </details>
  23. # 答案1
  24. **得分**: 4
  25. 如前所述,该服务器不支持HTTP2:
  26. ~~~
  27. PS C:\> curl.exe -I --http2-prior-knowledge https://webhook.site
  28. curl: (16) HTTP2帧层出现错误
  29. ~~~
  30. 与支持HTTP2的服务器相比:
  31. ~~~
  32. PS C:\> curl.exe -I --http2-prior-knowledge https://example.com
  33. HTTP/2 200
  34. ~~~
  35. https://curl.se/docs/manpage.html#--http2-prior-knowledge
  36. <details>
  37. <summary>英文:</summary>
  38. As was mentioned, that server doesn&#39;t support HTTP2:
  39. ~~~
  40. PS C:\&gt; curl.exe -I --http2-prior-knowledge https://webhook.site
  41. curl: (16) Error in the HTTP2 framing layer
  42. ~~~
  43. Contrast with one that does:
  44. ~~~
  45. PS C:\&gt; curl.exe -I --http2-prior-knowledge https://example.com
  46. HTTP/2 200
  47. ~~~
  48. https://curl.se/docs/manpage.html#--http2-prior-knowledge
  49. </details>

huangapple
  • 本文由 发表于 2021年5月18日 04:09:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/67576428.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定