簡體   English   中英

對等 GO 重置連接

[英]Connection reset by peer GO

我正在嘗試在 GO 中執行請求,但我總是收到“對等方重置連接”錯誤。 以下代碼顯示了我如何處理請求:

req, err := http.NewRequest("GET", url, nil)
if err != nil {
    return nil, err
}
client := = &http.Client{}
resp, err := client.Do(req)

if err != nil {
    return nil, err
}

defer resp.Body.Close()
fmt.Println(resp.Body)

...我收到:

Get https://example.com: read tcp 1.2.3.4:1234->5.6.7.8:5678: read: connection reset by peer

當我 curl https://example.com 時,我會收到來自服務器的響應。 為什么我不能在 GO 中執行請求?

如果我將它用於像https://example.com這樣的 URL,你的代碼就可以工作。 您確定要傳遞正確的 URL 嗎?

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

func main() {
    url := "https://example.com"
    req, err := http.NewRequest("GET", url, nil)
    if err != nil {
        fmt.Println(err)
        return
    }

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println(err)
        return
    }

    fmt.Println("OK")
    defer resp.Body.Close()

    bytes, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println(err)
        return
    }

    str := string(bytes[:])
    fmt.Printf("%s", str)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM