package httpClient import ( "bytes" "crypto/tls" "fmt" "io/ioutil" "net/http" "net/url" "strings" "time" ) const ( timeout = 5 * time.Second ) type HttpRequest struct { Url string Method string TimeOut time.Duration Values map[string]string Bytes []byte } type HttpRequestWithHead struct { Url string Method string Head map[string]string TimeOut time.Duration Query map[string]string Body []byte } func HttpPostWithHead(path string, head map[string]string, query map[string]string, body []byte) ([]byte, error) { if query != nil { path += joinGetRequestArgs(query) } h := HttpRequestWithHead{ Url: path, Method: http.MethodPost, Head: head, Body: body, TimeOut: timeout, } return h.request() } func HttpPutWithHead(path string, head map[string]string, query map[string]string, body []byte) ([]byte, error) { if query != nil { path += joinGetRequestArgs(query) } path += joinGetRequestArgs(query) h := HttpRequestWithHead{ Url: path, Method: http.MethodPut, Head: head, Body: body, TimeOut: timeout, } return h.request() } func HttpPost(path string, data map[string]string) ([]byte, error) { h := HttpRequest{ Url: path, Method: http.MethodPost, Values: data, TimeOut: timeout, } return h.request() } func HttpPostBytes(path string, data []byte) ([]byte, error) { h := HttpRequest{ Url: path, Method: http.MethodPost, TimeOut: timeout, Bytes: data, } return h.requestBytes() } func HttpPut(path string, data map[string]string) ([]byte, error) { h := HttpRequest{ Url: path, Method: http.MethodPut, Values: data, TimeOut: timeout, } return h.request() } func HttpDelete(path string, data map[string]string) ([]byte, error) { h := HttpRequest{ Url: path, Method: http.MethodDelete, Values: data, TimeOut: timeout, } return h.request() } func HttpGet(path string, data map[string]string) ([]byte, error) { path += joinGetRequestArgs(data) h := HttpRequest{ Url: path, Method: http.MethodGet, TimeOut: timeout, } return h.request() } func (h HttpRequest) request() ([]byte, error) { values := url.Values{} if h.Values != nil { for k, v := range h.Values { values.Add(k, v) } } request, err := http.NewRequest(h.Method, h.Url, strings.NewReader(values.Encode())) if h.Method == http.MethodPost || h.Method == http.MethodPut || h.Method == http.MethodDelete { request.Header.Add("Content-Type", "application/x-www-form-urlencoded") } //跳过证书验证 tr := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, } client := http.Client{ Transport: tr, Timeout: timeout, } resp, err := client.Do(request) if err != nil { return nil, err } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode) } contents, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } return contents, nil } func (h HttpRequestWithHead) request() ([]byte, error) { request, err := http.NewRequest(h.Method, h.Url, bytes.NewReader(h.Body)) if h.Method == http.MethodPost || h.Method == http.MethodPut || h.Method == http.MethodDelete { request.Header.Add("Content-Type", "application/json") } for k, v := range h.Head { request.Header.Add(k, v) } //跳过证书验证 tr := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, } client := http.Client{ Transport: tr, Timeout: timeout, } resp, err := client.Do(request) if err != nil { return nil, err } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode) } contents, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } return contents, nil } func (h HttpRequest) requestBytes() ([]byte, error) { request, err := http.NewRequest(h.Method, h.Url, bytes.NewReader(h.Bytes)) if h.Method == http.MethodPost || h.Method == http.MethodPut || h.Method == http.MethodDelete { request.Header.Add("Content-Type", "application/json") } //跳过证书验证 tr := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, } client := http.Client{ Transport: tr, Timeout: timeout, } resp, err := client.Do(request) if err != nil { return nil, err } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode) } contents, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } return contents, nil } func joinGetRequestArgs(data map[string]string) string { var path string if data != nil { path += "?" for k, v := range data { path += k + "=" + v + "&" } path = strings.TrimRight(path, "&") return path } return path }