使用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) }

内容的提问来源于stack exchange,提问作者Ashley John
相关产品推荐
相关产品推荐

