golang请求非安全http

通常情况下,我们可以简单使用Golang的`net/http`包进行http或https的请求。例如如下代码所示:
package main

import (
        "net/http"
        "time"
        "strings"
        "io/ioutil"
        "errors"
        "fmt"
)

func http_do(url string, method string, body string, header map[string]string) ([]byte, error) {
        // parameter check
        if url == "" || method == "" {
                return []byte{}, errors.New("param is invald")
        }
        // make the http client
        http_client := &http.Client{Timeout: 20 * time.Second}
        // make the request
        req, err := http.NewRequest(method, url, strings.NewReader(body))
        if err != nil {
                return nil, err
        }
        if header != nil {
                for key, val := range header {
                        req.Header.Add(key, val)
                }
        }
        // do a request
        resp, err := http_client.Do(req)
        if err != nil {
                return nil, err
        }

        defer resp.Body.Close()
        return ioutil.ReadAll(resp.Body)
}

func main() {
        url := "https://www.baidu.com"
        result, err := http_do(url,"GET","",nil)
        fmt.Printf("response:\n%v\nerror:\n%v\n", string(result), err)
}
运行以上代码,通常能正确的返回结果,除非你的计算机网络存在异常。但是对于我们自搭建的https后台,我们仍通过以上代码逻辑去访问的话,将有可能发生错误。例如,我们通过如下代码访问我们自建的F5服务将会发生错误:
package main

import (
        "net/http"
        "time"
        "strings"
        "io/ioutil"
        "errors"
        "fmt"
        "encoding/base64"
)

func http_do(url string, method string, body string, header map[string]string) ([]byte, error) {
        // parameter check
        if url == "" || method == "" {
                return []byte{}, errors.New("param is invald")
        }
        // make the http client
        http_client := &http.Client{Timeout: 20 * time.Second}
        // make the request
        req, err := http.NewRequest(method, url, strings.NewReader(body))
        if err != nil {
                return nil, err
        }
        if header != nil {
                for key, val := range header {
                        req.Header.Add(key, val)
                }
        }
        // do a request
        resp, err := http_client.Do(req)
        if err != nil {
                return nil, err
        }

        defer resp.Body.Close()
        return ioutil.ReadAll(resp.Body)
}

func main() {
        user   := "admin"
        passwd := "admin"
        auth_token := "Basic "
        auth_token += base64.StdEncoding.EncodeToString([]byte(user + ":" + passwd))
        url := "https://localhost/mgmt/tm/ltm/pool"
        header := map[string]string{"Authorization":auth_token}
        result, err := http_do(url,"GET","",header)
        fmt.Printf("response:\n%v\nerror:\n%v\n", string(result), err)
}
运行以上代码将得到错误信息:
Get https://localhost/mgmt/tm/ltm/pool: x509: cannot validate certificate for localhost because it doesn't contain any IP SANs
这是因为无法验证证书导致的错误,那么我们怎么访问这种非安全https请求呢?只需要在创建http client的时候,配置 `InsecureSkipVerify` `true` 即可。完整代码如下:
package main

import (
        "net/http"
        "time"
        "strings"
        "io/ioutil"
        "errors"
        "fmt"
        "encoding/base64"
        "crypto/tls"
)

func http_do(url string, method string, body string, header map[string]string) ([]byte, error) {
        // parameter check
        if url == "" || method == "" {
                return []byte{}, errors.New("param is invald")
        }
        // make the http client
        tr := &http.Transport{
                TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
        }
        http_client := &http.Client{Timeout: 20 * time.Second, Transport: tr}
        // make the request
        req, err := http.NewRequest(method, url, strings.NewReader(body))
        if err != nil {
                return nil, err
        }
        if header != nil {
                for key, val := range header {
                        req.Header.Add(key, val)
                }
        }
        // do a request
        resp, err := http_client.Do(req)
        if err != nil {
                return nil, err
        }

        defer resp.Body.Close()
        return ioutil.ReadAll(resp.Body)
}

func main() {
        user   := "admin"
        passwd := "admin"
        auth_token := "Basic "
        auth_token += base64.StdEncoding.EncodeToString([]byte(user + ":" + passwd))
        url := "https://localhost/mgmt/tm/ltm/pool"
        header := map[string]string{"Authorization":auth_token}
        result, err := http_do(url,"GET","",header)
        fmt.Printf("response:\n%v\nerror:\n%v\n", string(result), err)
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

樛木南雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值