登录
首页 >  Golang >  Go问答

读取状态码为 101的响应的消息体

来源:stackoverflow

时间:2024-02-24 12:54:25 501浏览 收藏

知识点掌握了,还需要不断练习才能熟练运用。下面golang学习网给大家带来一个Golang开发实战,手把手教大家学习《读取状态码为 101的响应的消息体》,在实现功能的过程中也带大家重新温习相关知识点,温故而知新,回头看看说不定又有不一样的感悟!

问题内容

我已连接到 kubernetes 集群中的服务器,并使用 post 请求和标头来升级请求。我正在使用以下功能:

func postrequest(client *http.client, url string, bodydata []byte) (*http.response, error){
    req, _ := http.newrequest("post", url, bytes.newbuffer(bodydata))
    //req.header.set("authorization", "bearer " + bearer_token)
    req.header.set("x-stream-protocol-version", "v2.channel.k8s.io")
    req.header.set("x-stream-protocol-version", "channel.k8s.io")
    req.header.set("upgrade", "spdy/3.1")
    req.header.set("connection","upgrade")
    req.header.set("content-type", "application/x-www-form-urlencoded")

    resp, err := (*client).do(req)

    return resp, err
}

收到回复后,我尝试阅读它,但在阅读正文时卡住了:

url2 := "https://<serveri_ip>:10250/exec/default/mypod/mycontainer?command=ls&command=/&input=1&output=1&tty=1"

resp, err := postrequest(api.globalclient, url2, []byte(""))
fmt.println(r.status)
fmt.println(r.header)
bodybytes, err := ioutil.readall(r.body) // -> it stuck here
fmt.println(string(bodybytes))

我想它试图打开 websocket,所以我尝试使用 gorilla websocket 库,如下所示:

u := url.URL{Scheme: "ws", Host: "<node_ip>:10250", Path: "/exec/default/mypod/mycontainer?command=ls&command=/&input=1&output=1&tty=1"}

interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)

c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
  log.Fatal("dial:", err)
}
defer c.Close()

但它打印了一个错误:

2020/04/04 20:51:25 dial:websocket: 握手错误

我做错了什么?

如何从“切换协议”状态读取响应正文


解决方案


我设法使用 kubernetes go-client 库做到了:

package main

import (
    "crypto/tls"
    "fmt"
    "net/http"
    "net/url"
    "os"
    "time"

    restclient "k8s.io/client-go/rest"
    "k8s.io/client-go/tools/remotecommand"
)


func main(){

    /*req.Header.Add("X-Stream-Protocol-Version", "v4.channel.k8s.io")
    req.Header.Add("X-Stream-Protocol-Version", "v3.channel.k8s.io")
    req.Header.Add("X-Stream-Protocol-Version", "v2.channel.k8s.io")
    req.Header.Add("X-Stream-Protocol-Version", "channel.k8s.io")
    req.Header.Add("Connection", "upgrade")
    req.Header.Add("Upgrade", "SPDY/3.1")*/
    //url2 := "https://123.123.123.123:10250/exec/default/my-pod/nginx?command=ls&command=/&input=1&output=1&tty=1"
    tr := &http.Transport{
        MaxIdleConns:       10,
        IdleConnTimeout:    30 * time.Second,
        DisableCompression: true,
        TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
    }

    config := &restclient.Config{
        Host:                "https://123.123.123.123:10250",
        APIPath:             "/exec/default/my-pod/nginx",
        TLSClientConfig:     restclient.TLSClientConfig{
            Insecure: true,
        },
        Transport:           tr,
    }

    url3 := &url.URL{
        Scheme:     "https",
        Opaque:     "",
        User:       nil,
        Host:       "123.123.123.123:10250",
        Path:       "/exec/default/my-pod/nginx",
        RawPath:    "",
        RawQuery:   "command=ls&command=/&input=1&output=1&tty=1",
    }
    exec, err := remotecommand.NewSPDYExecutor(config, "POST", url3)
    if err != nil {
        fmt.Println(err)
    }

    // Thanks for this blog post https://www.henryxieblogs.com/2019/05/
    err = exec.Stream(remotecommand.StreamOptions{
        Stdin:  os.Stdin,
        Stdout: os.Stdout,
        Stderr: os.Stderr,
        Tty:true,
    })

    fmt.Println(err)
}

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>