package minedata import ( "crypto/tls" "fmt" "io" "net/http" "net/url" "strings" ) func HttpDo(reqUrl, method string, params map[string]string, header map[string]string) (data []byte, err error) { var paramStr = "" for k, v := range params { if len(paramStr) == 0 { paramStr = fmt.Sprintf("%s=%s", k, url.QueryEscape(v)) } else { paramStr = fmt.Sprintf("%s&%s=%s", paramStr, k, url.QueryEscape(v)) } } client := &http.Client{ Transport: &http.Transport{ //对客户端进行一些配置 TLSClientConfig: &tls.Config{ InsecureSkipVerify: true, }, }, } var req *http.Request if strings.ToUpper(method) == "GET" { reqUrl += "?" + paramStr req, err = http.NewRequest(strings.ToUpper(method), reqUrl, nil) } else { req, err = http.NewRequest(strings.ToUpper(method), reqUrl, strings.NewReader(paramStr)) } req.Header.Set("content-type", "X-WWW-FORM-URLENCODED") for k, v := range header { req.Header.Set(k, v) } resp, err := client.Do(req) defer func() { _ = resp.Body.Close() }() body, err := io.ReadAll(resp.Body) if err != nil { return nil, err } return body, nil }