golang中http请求

栏目: Go · 发布时间: 6年前

var httpSerever *http.Client = &http.Client{}
func HttpRequest(method, url string, headers map[string]string) ([]byte, error) {
    req, err := http.NewRequest(method, url, nil)
    if err != nil {
        return nil, err
    }
    if headers != nil && len(headers) != 0 {
        for k, v := range headers{
            req.Header.Add(k, v)
        }
    }
    respBytes, err := httpDo(req)
    return respBytes, err
}
func httpDo(req *http.Request) ([]byte, error) {
    resp, err := httpSerever.Do(req)
    if err != nil {
        return nil, errors.New("RetryMarkup" + err.Error())
    }
    defer resp.Body.Close()
    if resp.StatusCode != http.StatusOK {
        return nil, fmt.Errorf("RetryMarkup http get error : url=%v , statusCode=%v", req.URL, resp.StatusCode)
    }
    return ioutil.ReadAll(resp.Body)
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Flow-Based Programming (2nd Edition)

Flow-Based Programming (2nd Edition)

CreateSpace / 2010-5-14 / $69.95

Written by a pioneer in the field, this is a thorough guide to the cost- and time-saving advantages of Flow-Based Programming. It explains the theoretical underpinnings and application of this program......一起来看看 《Flow-Based Programming (2nd Edition)》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

URL 编码/解码
URL 编码/解码

URL 编码/解码