如何强制客户端使用http / 2? (而不是回到http 1.1)

时间:2018-11-19 03:11:30

标签: go https client http2

如何强制一个简单的Go客户端使用HTTP / 2并防止其回退到HTTP 1.1?

我有一个在“ localhost”上运行的简单HTTP / 2服务器,它在答复中返回请求的详细信息。以下是使用Google Chrome浏览器对此网址进行的输出: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客户端请求。

1 个答案:

答案 0 :(得分:1)

首先导入"golang.org/x/net/http2"软件包。然后更改

transport := &http.Transport{TLSClientConfig: tlsConfig}

transport := &http2.Transport{TLSClientConfig: tlsConfig}