golang中http请求

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

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)
}

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

查看所有标签

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

Rapid Web Applications with TurboGears

Rapid Web Applications with TurboGears

Mark Ramm、Kevin Dangoor、Gigi Sayfan / Prentice Hall PTR / 2006-11-07 / USD 44.99

"Dear PHP, It's over between us. You can keep the kitchen sink, but I want my MVC. With TurboGears, I was able to shed the most heinous FileMaker Pro legacy 'solu-tion' imaginable. It has relationshi......一起来看看 《Rapid Web Applications with TurboGears》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试