hunter-ji / Blog

My Blog.
121 stars 38 forks source link

Go http请求报错x509 certificate signed by unknown authority #13

Open hunter-ji opened 4 years ago

hunter-ji commented 4 years ago

报错

在Go中POST请求时报错

x509: certificate signed by unknown authority

即无法检验证书。

package main

import (
    "net/http"
)

func Handle() {

 ...

    _, err := http.Post(
        ...
    )

...

}

解决

跳过校验即可。此处引入"crypto/tls"

package main

import (
  "crypto/tls"
    "net/http"
)

func Handle() {

    ...

    tr := &http.Transport{
        TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
    }

    client := &http.Client{
        Timeout:   15 * time.Second,
        Transport: tr,
    }

    _, err := client.Post(
        ...
    )

    ...
}