我有一个使用HTTP / 2的基本客户端和服务器实现。我也想测试服务器也可以使用HTTP / 1。

有什么方法可以将协议从HTTP / 2更改为HTTP / 1.x?

客户代码:

func main() {
  host = "https://127.0.0.1:8080"
  client = http.Client{

    // InsecureTLSDial is temporary and will likely be
    // replaced by a different API later.
    Transport: &http2.Transport{
        TLSClientConfig: &tls.Config{
            InsecureSkipVerify: true,
        },
    },
  }

  // further functionality
}

服务器代码:
func main() {

    var srv http.Server
    srv.Addr = ":8080"

    // Set Routes
    routes()

    // Start server
    srv.ListenAndServeTLS("certs/localhost.cert", "certs/localhost.key")
}

最佳答案

我找到了一个简单的解决方案。

改变客户

Transport: &http2.Transport{
    TLSClientConfig: &tls.Config{
        InsecureSkipVerify: true,
    },
},

通过
Transport: &http.Transport{
        TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
        TLSClientConfig: &tls.Config{
            InsecureSkipVerify: true,
        },
    },

关于http - 如何在客户端和服务器中使用HTTP/1.x,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54401790/

10-09 15:18
查看更多