You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Golang建立HTTPS连接失败,但curl与C#可成功连接

Golang建立HTTPS连接时被远程主机强制关闭连接的问题

用Golang发起HTTPS连接时,持续出现错误:"An existing connection was forcibly closed by remote host"(远程主机强制关闭了现有连接)。同一台机器上,使用curl或C#.NET的HttpClient访问同一个端点均能成功建立连接,但Golang脚本却触发该错误。

抓取TCP数据包后发现:使用Golang请求时,服务器会设置RST位导致连接重置;而C#的HttpClient请求时不会出现此问题。

以下是使用的Golang代码:

url := ""

client := &http.Client{
    Timeout: 5 * time.Second,

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


for i := 0; i < 5; i++ {
    resp, err := client.Get(url)
    if err != nil {
        fmt.Printf("Error sending request #%d: %v\n", i+1, err)
        continue
    }
    defer resp.Body.Close()
    fmt.Printf("Request #%d sent, Status Code: %d\n", i+1, resp.StatusCode)
}

TCP数据包对比

内容的提问来源于stack exchange,提问作者Ashley John

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 17:20:11