如何强制一个简单的 Go 客户端使用 HTTP/2 并防止它退回到 HTTP 1.1?
我有一个在“本地主机”上运行的简单 HTTP/2 服务器,它在回复中返回请求的详细信息。这是使用 Google Chrome 浏览器对此 URL 的输出:https://localhost:40443/bananas
I like bananas!
Method = GET
URL = /bananas
Proto = HTTP/2.0
Host = localhost:40443
RequestURI = /bananas
但这是我为我的 Go 客户端代码得到的。你可以看到它回落到 HTTP 1.1
I like monkeys!
Method = GET
URL = /monkeys
Proto = HTTP/1.1
Host = localhost:40443
RequestURI = /monkeys
下面是我使用 HTTP/2 连接同一服务器的最佳尝试的源代码,但它总是回退到 HTTP 1.1
// simple http/2 client
package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"log"
"net/http"
)
const (
certFile = "client-cert.pem"
keyFile = "client-key.pem"
caFile = "server-cert.pem"
)
func main() {
// Load client certificate
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
log.Fatal(err)
}
// Load CA cert
caCert, err := ioutil.ReadFile(caFile)
if err != nil {
log.Fatal(err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
// Setup HTTPS client
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: caCertPool,
}
tlsConfig.BuildNameToCertificate()
transport := &http.Transport{TLSClientConfig: tlsConfig}
client := &http.Client{Transport: transport}
response, err := client.Get("https://localhost:40443/monkeys")
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
// dump response
text, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Body:\n%s", text)
}
任何建议,包括指向其他工作示例的指针,这些示例说明如何在 Go 中发出 HTTP/2 客户端请求,我们将不胜感激。
慕姐4208626
服务器让它监听客户端的连接用accept()方法,那么客户端如何监听呢?
Cookie是存储在客户端浏览器中的数据。为什么一般情况下,Cookie通过HTTP headers从服务端返回到客户端?
如何通过代码注册用户,而不是在服务器的客户端手动输入?谢谢老师回答
客户端如何安装?
相关分类