http
This commit is contained in:
parent
64fe0dfafb
commit
0c181e1796
30
http.go
30
http.go
|
@ -11,25 +11,25 @@ import (
|
|||
)
|
||||
|
||||
// PostJson 请求
|
||||
func HttpPost(link string, params map[string]string, json []byte) ([]byte, error) {
|
||||
func HttpPost(link string, header map[string]string, json []byte) ([]byte, error) {
|
||||
client := &http.Client{Timeout: 20 * time.Second}
|
||||
//忽略https的证书
|
||||
client.Transport = &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
}
|
||||
p := url.Values{}
|
||||
|
||||
u, _ := url.Parse(link)
|
||||
if params != nil {
|
||||
for k, v := range params {
|
||||
p.Set(k, v)
|
||||
}
|
||||
}
|
||||
u.RawQuery = p.Encode()
|
||||
|
||||
req, err := http.NewRequest("POST", u.String(), bytes.NewBuffer(json))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
if header != nil {
|
||||
for k, v := range header {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
|
@ -43,24 +43,22 @@ func HttpPost(link string, params map[string]string, json []byte) ([]byte, error
|
|||
}
|
||||
|
||||
// Get 请求 link:请求url
|
||||
func HttpGet(link string, params map[string]string) ([]byte, error) {
|
||||
func HttpGet(link string, header map[string]string) ([]byte, error) {
|
||||
client := &http.Client{Timeout: 20 * time.Second}
|
||||
//忽略https的证书
|
||||
client.Transport = &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
}
|
||||
p := url.Values{}
|
||||
u, _ := url.Parse(link)
|
||||
if params != nil {
|
||||
for k, v := range params {
|
||||
p.Set(k, v)
|
||||
}
|
||||
}
|
||||
u.RawQuery = p.Encode()
|
||||
req, err := http.NewRequest("GET", u.String(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if header != nil {
|
||||
for k, v := range header {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
Loading…
Reference in New Issue