44 lines
829 B
Go
44 lines
829 B
Go
|
|
package tools
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"io/ioutil"
|
|||
|
|
"net/http"
|
|||
|
|
"strings"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
/*
|
|||
|
|
请求示例:
|
|||
|
|
url := "https://blog.csdn.net/weixin_60232039/article/details/128861515"
|
|||
|
|
headers := make(map[string]string)
|
|||
|
|
headers["Content-Type"] = "application/json"
|
|||
|
|
meth := "get"
|
|||
|
|
err, bytes := GetPost(url, meth, headers, nil)
|
|||
|
|
|
|||
|
|
if err != nil {
|
|||
|
|
panic(err)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// fmt.Println(string(bytes))
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
func GetPost(url string, meth string, header map[string]string, data []byte) (error, []byte) {
|
|||
|
|
|
|||
|
|
client := &http.Client{}
|
|||
|
|
newMeth := strings.ToUpper(meth)
|
|||
|
|
req, err := http.NewRequest(newMeth, url, strings.NewReader(string(data)))
|
|||
|
|
if err != nil {
|
|||
|
|
return err, nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if len(header) > 0 {
|
|||
|
|
for k, v := range header {
|
|||
|
|
req.Header.Add(k, v)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
resp, _ := client.Do(req)
|
|||
|
|
defer resp.Body.Close()
|
|||
|
|
body, _ := ioutil.ReadAll(resp.Body)
|
|||
|
|
return nil, body
|
|||
|
|
}
|