Avatar ·

如何在Go中执行带有自签名或无效证书的HTTPS请求?

大家好!我是Go新手,正在做一个需要向内部开发服务器发送HTTPS请求的项目。问题在于该服务器使用了自签名SSL证书,而Go默认会拒绝此类连接,并报错x509: certificate signed by unknown authority。

我当前的代码如下:

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

func main() {
    resp, err := http.Get("https://internal-dev.example.com/api/data")
    if err != nil {
        fmt.Println("错误:", err)
        return
    }
    defer resp.Body.Close()
    
    body, _ := ioutil.ReadAll(resp.Body)
    fmt.Println(string(body))
}

我明白在生产环境中这样做是不好的实践,但为了本地开发,我需要临时忽略证书验证。我尝试查阅文档,但里面有很多复杂的结构,比如http.Transport和tls.Confi