Compare commits

..

No commits in common. "9e09eece6f4039ea5f62a1b720b6e569747da2c3" and "f4c1cdb28331e11315cc5f009614f3d54c7289b1" have entirely different histories.

1 changed files with 44 additions and 33 deletions

View File

@ -9,36 +9,26 @@ import (
"time"
)
var (
httpClient *http.Client
)
func init() {
httpClient = &http.Client{
Timeout: 20 * time.Second,
Transport: &http.Transport{
MaxIdleConns: 200,
MaxIdleConnsPerHost: 50,
MaxConnsPerHost: 500,
IdleConnTimeout: 90 * time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
}
// PostJson 请求
func HttpPostJson(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},
}
req, err := http.NewRequest("POST", link, bytes.NewBuffer(json))
if err != nil {
return nil, err
}
if header != nil {
for k, v := range header {
req.Header.Add(k, v)
}
}
req.Header.Add("Content-Type", "application/json")
resp, err := httpClient.Do(req)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
@ -51,17 +41,24 @@ func HttpPostJson(link string, header map[string]string, json []byte) ([]byte, e
// PostJson 请求
func HttpPutJson(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},
}
req, err := http.NewRequest("PUT", link, bytes.NewBuffer(json))
if err != nil {
return nil, err
}
if header != nil {
for k, v := range header {
req.Header.Add(k, v)
}
}
req.Header.Add("Content-Type", "application/json")
resp, err := httpClient.Do(req)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
@ -74,15 +71,22 @@ func HttpPutJson(link string, header map[string]string, json []byte) ([]byte, er
// Get 请求 link请求url
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},
}
req, err := http.NewRequest("GET", link, nil)
if err != nil {
return nil, err
}
if header != nil {
for k, v := range header {
req.Header.Add(k, v)
}
resp, err := httpClient.Do(req)
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
@ -95,15 +99,22 @@ func HttpGet(link string, header map[string]string) ([]byte, error) {
// Get 请求 link请求url
func HttpDelete(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},
}
req, err := http.NewRequest("DELETE", link, nil)
if err != nil {
return nil, err
}
if header != nil {
for k, v := range header {
req.Header.Add(k, v)
}
resp, err := httpClient.Do(req)
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}