golang https x509: certificate signed by unknown authority解决
某管理口页面使用的证书文件是不被浏览器识别的,在使用python requests进行页面请求时,在linux下忽略ssl正常,在windos使用就提示SSLv3 handler error,所以改用golang进行页面访问,结果提示“x509: certificate signed by unknown authority”错误。使用golang代码类似如下:
1package main
2import (
3 "fmt"
4 "io/ioutil"
5 "net/http"
6)
7func main() {
8 resp, err := http.Get("https://localhost:8081")
9 if err != nil {
10 fmt.Println("error:", err)
11 return
12 }
13 defer resp.Body.Close()
14 body, err := ioutil.ReadAll(resp.Body)
15 fmt.Println(string(body))
16}
网上查了下,可以在认证的时候,忽略证书校验,具体操作如下:
1package main
2import (
3 "crypto/tls"
4 "fmt"
5 "io/ioutil"
6 "net/http"
7)
8func main() {
9 tr := &http.Transport{
10 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
11 }
12 client := &http.Client{Transport: tr}
13 resp, err := client.Get("https://localhost:8081")
14 if err != nil {
15 fmt.Println("error:", err)
16 return
17 }
18 defer resp.Body.Close()
19 body, err := ioutil.ReadAll(resp.Body)
20 fmt.Println(string(body))
21}
通过设置tls.Config的InsecureSkipVerify为true,client将不再对服务端的证书进行校验。就可以正常获取结果了,同样post请求也是知用的。
捐赠本站(Donate)
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
- Author: shisekong
- Link: https://blog.361way.com/golang-https-509/6102.html
- License: This work is under a 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. Kindly fulfill the requirements of the aforementioned License when adapting or creating a derivative of this work.