美文网首页
golang常用代码片段--定制化http-client组件

golang常用代码片段--定制化http-client组件

作者: 江江的大猪 | 来源:发表于2019-11-24 23:43 被阅读0次
  • 封装标准库的http工具
import (
    "crypto/tls"
    "errors"
    "github.com/gin-gonic/gin"
    "io/ioutil"
    "net/http"
    "strconv"
    "strings"
    "time"
)

var httpClient = &http.Client{
    Timeout: 3 * time.Second,
    // 返回301、302重定向时,不会自动发起重定向访问
    CheckRedirect: func(req *http.Request, via []*http.Request) error {
        return http.ErrUseLastResponse
    },
    Transport: &http.Transport{
        TLSClientConfig: &tls.Config{
            // 不校验https证书
            InsecureSkipVerify: true,
        },
        MaxConnsPerHost:     300,
        MaxIdleConns:        150,
        MaxIdleConnsPerHost: 75,
        IdleConnTimeout:     10 * time.Second,
    },
}

func HttpPostWithRetry(url, body string, retryTime int) (result []byte, err error) {
    if retryTime < 0 {
        return nil, errors.New("HttpPostWithRetry retryTime is " + strconv.Itoa(retryTime))
    }
    for i := 0; i < retryTime; i++ {
        if result, err = HttpPost(url, body); err == nil {
            break
        }
    }
    return
}

func HttpGetWithRetry(url string, retryTime int) (result []byte, err error) {
    if retryTime < 0 {
        return nil, errors.New("HttpGetWithRetry retryTime is " + strconv.Itoa(retryTime))
    }
    for i := 0; i < retryTime; i++ {
        if result, err = HttpGet(url); err == nil {
            break
        }
    }
    return
}

func HttpPost(url, body string) ([]byte, error) {
    resp, err := httpClient.Post(url, gin.MIMEPOSTForm, strings.NewReader(body))
    // 重定向时,resp不为nil,如果CheckRedirect返回非ErrUseLastResponse错误,err也不为nil
    if resp != nil {
        defer resp.Body.Close()
    }
    if err != nil {
        return nil, err
    }
    return ioutil.ReadAll(resp.Body)
}

func HttpGet(url string) ([]byte, error) {
    resp, err := httpClient.Get(url)
    // 重定向时,resp不为nil,如果CheckRedirect返回非ErrUseLastResponse错误,err也不为nil
    if resp != nil {
        defer resp.Body.Close()
    }
    if err != nil {
        return nil, err
    }
    return ioutil.ReadAll(resp.Body)
}

相关文章

网友评论

      本文标题:golang常用代码片段--定制化http-client组件

      本文链接:https://www.haomeiwen.com/subject/ixnrwctx.html