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)
}
有疑问加站长微信联系(非本文作者)