通过HTTP2运输获得意外的EOF
在GO中尝试HTTP2软件包,在创建HTTP2连接时,我会遇到意外的EOF错误。无法找出确切的问题。
tcpConn, err := net.Dial("tcp", "clients1.google.com:443")
if err != nil {
panic(err)
}
defer tcpConn.Close()
t := http2.Transport{}
http2ClientConn, err := t.NewClientConn(tcpConn)
if err != nil {
panic(err)
}
req, err := http.NewRequest("GET", "https://clients1.google.com/generate_204", nil)
if err != nil {
panic(err)
}
resp, err := http2ClientConn.RoundTrip(req)
if err != nil {
panic(err) // getting unexpected EOF
}
defer resp.Body.Close()
fmt.Println(resp.StatusCode)
输出
panic: unexpected EOF
goroutine 1 [running]:
main.main()
/Users/rishabharya/Desktop/Projects/src/github.com/rishabh-arya95/raw_http/main.go:31 +0x217
exit status 2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用 tls 用应用程序层协议协议协议(alpn)。
会发生什么?
客户需要更改的
客户建立了与 standard htttps 端口(端口号为 443 ),但使用 net.dial()函数。 https 协议使用 通过Internet保护HTTP连接。因此,客户必须使用 tls.dial()函数来建立连接:
但是,客户端不能使用空的 tls配置。在这种特殊情况下,服务器支持这两个协议: http/1.1 =“ https://datatracker.ietf.org/doc/html/rfc2818” rel =“ nofollow noreferrer”> http/2 。默认情况下,服务器使用HTTP/1.1协议。请求使用http/2协议有两种方法:
第二种方法不合适。第一个原因是 http2 软件包不打算为此。第二个原因是服务器忽略了给定请求的HTTP升级机制。
为了使用第一个方法,我们需要添加“ H2” “ a> to受支持的应用级协议列表(所有已注册的标识符可以在
工作代码
和略修改版本。
curl 是一个方便的检查工具。
进行http/2请求:
禁用应用程序层协议协议(ALPN):
在没有应用程序层协议协议(ALPN)的情况下发送HTTP/2请求:
错误 52 :服务器没有回复任何内容,在这里被认为是错误。
You should use TLS encryption with Application-Layer Protocol Negotiation (ALPN).
What happens?
What needs to be change
The client establishes a connection to the standard HTTPS port (port number is 443), but use net.Dial() function. HTTPS protocol use TLS to secure HTTP connection over the Internet. Therefore, the client have to use tls.Dial() function to establish a connection:
However, the client cannot use an empty TLS configuration. In this particular case, the server supports both protocols: HTTP/1.1 and HTTP/2. By default, the server uses HTTP/1.1 protocol. There are two ways to request the use of HTTP/2 protocol:
The second method is not suitable. The first reason is that http2 package is not intended for this. The second reason is that the server ignores HTTP Upgrade mechanism for the given request.
In order to use the first methods, we need to add "h2" identifier to a list of supported application level protocols (a list of all registered identifiers can be found on IANA website).
Working Code
And a slightly modified version.
cURL is a handy tool for checking.
Make an HTTP/2 request:
Disable Application-Layer Protocol Negotiation (ALPN):
Send an HTTP/2 request without Application-Layer Protocol Negotiation (ALPN):
Error 52: The server did not reply anything, which here is considered an error.