猿问

尝试从解析的原始 HTTP 请求发送请求时出现意外的 EOF

我使用 golang 来解析原始 HTTP 请求,然后将请求发送到 URL 并打印输出。


法典:


stra := `GET / HTTP/1.1

Host: google.com

Upgrade-Insecure-Requests: 1

User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9

Accept-Encoding: gzip, deflate

Accept-Language: en-US,en;q=0.9

Connection: close`

    r, err := http.ReadRequest(bufio.NewReader(strings.NewReader(stra)))

    if err != nil {

        fmt.Println(err.Error())

        os.Exit(1)

    }

    u, err := url.Parse("https://" + r.Host + r.URL.String())

    if err != nil {

        panic(err)

    }

    req.URL = u

    transport := http.Transport{}

    transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}

    client := &http.Client{Transport: &transport}

    resp, err := client.Do(req)

错误:


unexpected EOF

请帮忙。


蛊毒传说
浏览 224回答 1
1回答

拉莫斯之舞

http 标头必须以 结尾,但似乎可以用于 ?下面的代码确实有效。\r\n\r\n\nhttp.ReadRequestpackage mainimport (    "bufio"    "fmt"    "net/http"    "os"    "strings")func main() {    stra := `GET / HTTP/1.1Host: google.comUpgrade-Insecure-Requests: 1User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9Accept-Encoding: gzip, deflateAccept-Language: en-US,en;q=0.9Connection: close`    req, err := http.ReadRequest(bufio.NewReader(strings.NewReader(stra)))    if err != nil {        fmt.Println(err.Error())        os.Exit(1)    }    fmt.Println(req)}
随时随地看视频慕课网APP

相关分类

Go
我要回答