-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
executable file
·112 lines (96 loc) · 2.33 KB
/
http.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package gutils
import (
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
)
var DefaultClient *http.Client
func DoGet(url string) (string, error) {
return DoGetWithHeader(url, nil)
}
func DoGetWithHeader(url string, header map[string]string) (string, error) {
b, err := doWithHeader(url, http.MethodGet, "", header)
if err != nil {
return "", err
}
return string(b), nil
}
func DoPost(url, body string) (string, error) {
return DoPostWithHeader(url, body, nil)
}
func DoPostWithHeader(url, body string, header map[string]string) (string, error) {
b, err := doWithHeader(url, http.MethodPost, body, header)
if err != nil {
return "", err
}
return string(b), nil
}
func DoPut(url, body string) (string, error) {
return DoPutWithHeader(url, body, nil)
}
func DoPutWithHeader(url, body string, header map[string]string) (string, error) {
b, err := doWithHeader(url, http.MethodPut, body, header)
if err != nil {
return "", err
}
return string(b), nil
}
// 通过代理Get请求
func DoProxyGet(urls, proxyIP string) (string, error) {
proxy, _ := url.Parse(proxyIP)
client := http.Client{
Transport: &http.Transport{
MaxIdleConns: 10,
MaxIdleConnsPerHost: 10,
Proxy: http.ProxyURL(proxy),
},
Timeout: time.Second * 5}
resp, err := client.Get(urls)
if err != nil {
return "", err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(data), nil
}
// method 支持GET, POST等方式,应该传常量,而不是直接传字符串,参考http.MethodGet
func doWithHeader(url, method, body string, header map[string]string) ([]byte, error) {
var bodyReader io.Reader
if len(body) > 0 {
bodyReader = strings.NewReader(body)
}
req, err := http.NewRequest(method, url, bodyReader)
if err != nil {
return nil, err
}
if header != nil && len(header) > 0 {
for k, v := range header {
req.Header.Set(k, v)
}
}
resp, err := DefaultClient.Do(req)
if err != nil {
return nil, err
}
// defer DefaultClient.CloseIdleConnections()
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return data, nil
}
func init() {
DefaultClient = &http.Client{
Transport: &http.Transport{
MaxIdleConns: 10,
MaxIdleConnsPerHost: 10,
},
Timeout: time.Second * 5}
}