扣丁书屋

Golang忽略HTTPS证书

1年以前  |  阅读数:455 次  |    

通过设置TLSClientConfig: &tls.Config{InsecureSkipVerify: true},来取消对HTTPS的证书验证,以处理x509: certificate signed by unknown authority

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

通过设置TLSClientConfig: &tls.Config{InsecureSkipVerify: true},来取消对HTTPS的证书验证,以处理如下错误信息:

error: Get https://localhost:8081: x509: certificate signed by unknown authority

相关文章: