解决golang https请求提示x509: certificate signed by unknown authority

使用golang做一个小爬虫的时候,目标URL是一个使用了自签证书的https站点,在请求时,报错如下:

Get https://x.x.x.x/cgi-bin/showflux.cgi: x509: certificate signed by unknown authority
panic: Get https://x.x.x.x/cgi-bin/showflux.cgi: x509: certificate signed by unknown authority

仅仅为了爬取内容而言,可以使用以下方法解决

步骤一:
引入 "crypto/tls" 包

步骤二:
定义TLSClientConfig,忽略证书校验:

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

步骤三:
修改

client := &http.Client{}

client := &http.Client{Transport: tr}

标签: golang