我试图在 Go 中发出一个简单的 HTTP 请求,在直接按照指南进行操作后,我不断收到相同的错误:
本地错误:tls:没有重新协商
我不是很明白这个怎么解释?我知道这在服务器上不是问题,因为当我从 python 调用相同的请求时它返回正常。这是我的代码:
package main
import (
"fmt"
"net/http"
"net/url"
"strings"
"time"
)
func main() {
timeout := time.Duration(20 * time.Second)
client := &http.Client{
Timeout: timeout,
}
data := url.Values{
"top": {"10"},
"lDate": {"2019-01-01"},
}
req, err := http.NewRequest("POST", "https://api.*********.com/AppAvailLoads?", strings.NewReader(data.Encode()))
if err != nil {
fmt.Println("Error in construction")
}
req.Header.Add("x-cdata-authtoken", "********")
req.Header.Add("content-type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error in request")
fmt.Println(err)
} else {
fmt.Println(resp.Body)
resp.Body.Close()
}
}
最佳答案
解决方案是启用 TLS 重新协商(我知道,请看图),这是一个常量,是 tls 包的一部分,如下所示:
tr := &http.Transport{
TLSClientConfig: &tls.Config{
Renegotiation: tls.RenegotiateOnceAsClient,
// You may need this if connecting to servers with self-signed certificates
// InsecureSkipVerify: true,
},
}
client := &http.Client{
Timeout: timeout,
Transport: tr,
}
这很奇怪,因为没有在线指南对此进行解释,也没有示例说明如何发生常见错误,例如 local error: tls: no renegotiation
。我希望这对来自其他语言的人有用,因为这不是人们通常处理的事情!
关于go - tls : no renegotiation error on HTTP request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57420833/