golang https x509: certificate signed by unknown authority解决

1760次阅读  |  发布于4年以前

某管理口页面使用的证书文件是不被浏览器识别的,在使用python requests进行页面请求时,在linux下忽略ssl正常,在windos使用就提示SSLv3 handler error,所以改用golang进行页面访问,结果提示“x509: certificate signed by unknown authority”错误。使用golang代码类似如下:

package main
import (
    "fmt"
    "io/ioutil"
    "net/http"
)
func main() {
    resp, err := http.Get("https://localhost:8081")
    if err != nil {
        fmt.Println("error:", err)
        return
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    fmt.Println(string(body))
}

网上查了下,可以在认证的时候,忽略证书校验,具体操作如下:

package main
import (
    "crypto/tls"
    "fmt"
    "io/ioutil"
    "net/http"
)
func main() {
    tr := &http.Transport{
        TLSClientConfig:    &tls.Config{InsecureSkipVerify: true},
    }
    client := &http.Client{Transport: tr}
    resp, err := client.Get("https://localhost:8081")
    if err != nil {
        fmt.Println("error:", err)
        return
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    fmt.Println(string(body))
}

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8