-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
61 lines (54 loc) · 1.11 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package tools
import (
"crypto/tls"
"fmt"
"io"
"net/http"
"net/url"
"srun4-pay/configs"
)
var (
Client *http.Client
)
func init() {
if Client == nil {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
Client = &http.Client{
Transport: tr,
}
}
}
func Get(url string) ([]byte, error) {
resp, err := Client.Get(url)
if err != nil {
configs.Log.WithField("HTTP GET ERROR", err).Error()
return nil, err
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
res, err := io.ReadAll(resp.Body)
if err != nil {
configs.Log.WithField("读取 GET Response 失败", err).Error()
return nil, err
}
return res, nil
}
func Post(url string, params url.Values) ([]byte, error) {
resp, err := Client.PostForm(url, params)
if err != nil {
configs.Log.WithField(fmt.Sprintf("HTTP POST[%s] ERROR", url), err).Error()
return nil, err
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
res, err := io.ReadAll(resp.Body)
if err != nil {
configs.Log.WithField("读取 POST Response 失败", err).Error()
return nil, err
}
return res, nil
}