package network import ( "io/ioutil" "net/http" "strings" ) /** * 自定义HTTP请求 */ func MethodHttp(method, url, param string, header ...map[string]string) ([]byte, error) { httpClient := &http.Client{} req, err := http.NewRequest(method, url, strings.NewReader(param)) if err != nil { return []byte(""), err } req.Header.Set("Content-Type", "application/x-www-form-urlencoded") if len(header) > 0 { for _, item := range header { for k, v := range item { req.Header.Set(k, v) } } } resp, err := httpClient.Do(req) if err != nil { return []byte(""), err } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { return []byte(""), err } return body, nil } /** * 自定义HTTP请求 */ func SendHttp(method, url string, param map[string]string, header ...map[string]string) ([]byte, error) { httpClient := &http.Client{} paramStr := "" if len(param) > 0 { for key, value := range param { paramStr += key + "=" + value + "&" } paramStr = paramStr[0 : len(paramStr)-1] } req, err := http.NewRequest(method, url, strings.NewReader(paramStr)) if err != nil { return []byte(""), err } req.Header.Set("Content-Type", "application/x-www-form-urlencoded") if len(header) > 0 { for _, item := range header { for k, v := range item { req.Header.Set(k, v) } } } resp, err := httpClient.Do(req) if err != nil { return []byte(""), err } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { return []byte(""), err } return body, nil } /** * send xml */ func SendXml(method, url string, requestxml interface{}, header ...map[string]string) ([]byte, error) { bytexml, err := xml.Marshal(&requestxml) if err != nil { return []byte(""), err } httpClient := &http.Client{} req, err := http.NewRequest(method, url, bytes.NewBuffer(param)) if err != nil { return []byte(""), err } req.Header.Add("Content-Type", "application/xml; charset=utf-8") if len(header) > 0 { for _, item := range header { for k, v := range item { req.Header.Add(k, v) } } } resp, err := httpClient.Do(req) if err != nil { return []byte(""), err } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { return []byte(""), err } return body, nil }