【问题标题】:How to write/read/send a data Frame using HTTP/2 in Golang?如何在 Golang 中使用 HTTP/2 写入/读取/发送数据帧?
【发布时间】:2016-11-12 20:14:33
【问题描述】:

我想知道如何使用 HTTP/2 来编写、读取和发送帧,例如数据帧。我知道 Golang 库 net/http 支持这个新协议,但我不知道如何正确地做上述方面。

提前谢谢你!

【问题讨论】:

    标签: go network-programming http2


    【解决方案1】:

    尝试像这样发送http2请求

    首先需要导入http2包

    import "golang.org/x/net/http2"
    

    然后,写一些请求代码

    t := &http2.Transport{}
    c := &http.Client{
        Transport: t,
    }
    r, _ := http.NewRequest("GET", "https://http2.golang.org/reqinfo", bytes.NewBuffer([]byte("hello")))
    resp, err := c.Do(r)
    if err != nil {
        fmt.Printf("request error")
    }
    defer resp.Body.Close()
    content, _ := ioutil.ReadAll(resp.Body)
    fmt.Printf("body length:%d\n", len(content))
    

    【讨论】:

    • 您没有在定义后的任何地方使用变量“t”和“r”。那么你为什么要定义它们呢?
    • t 用作http.Client 中的传输,r 用于调用c.Do(r) 并表示请求。
    猜你喜欢
    • 2019-01-29
    • 2017-10-06
    • 2016-07-25
    • 1970-01-01
    • 1970-01-01
    • 2017-01-29
    • 1970-01-01
    • 1970-01-01
    • 2018-04-30
    相关资源
    最近更新 更多