golang 访问HTTPS请求跳过证书验证

import "net/http"
func main(){
        url := "https://XXXXX"
        user := XXXX
        paasswd := XXXX
        payload := strings.NewReader("{\n\t\"username\":" + `"` + user + `"` + ",\n\t\"password\":" + `"` + passwd + `"` + "\n}")
        req, err := http.NewRequest("POST", url, payload)
        if err != nil {
                fmt.Println(err)
        }
        req.Header.Add("Content-Type", "application/json;charset=UTF8")
        //跳过证书验证
        tr := &http.Transport{
                TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
        }
        client := &http.Client{Timeout: 30 * time.Second, Transport: tr}
        resp, err := client.Do(req)
        if err != nil {
                fmt.Println(err)
        }
        defer resp.Body.Close()
        body, err := ioutil.ReadAll(resp.Body)
        if resp.StatusCode != 200 {
                return fmt.Errorf("failed to get token, the response code is %d, the resp is: \n%s", resp.StatusCode, string(body))
        }
        var res Res
        if err := json.Unmarshal([]byte(body), &res); err != nil {
                fmt.Println(err)
        }
        fmt.Println(res)
}

 

你可能感兴趣的:(go,云开发)