-
Notifications
You must be signed in to change notification settings - Fork 4
/
httpx.go
228 lines (198 loc) · 4.93 KB
/
httpx.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package gcode
import (
"errors"
"io"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
//最后一次请求时间
var last_request_time time.Time
var headerUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36"
type Httpx struct {
Url string
Headers map[string]string
Cookies []*http.Cookie
ClientIP string //本机外网IP,可选
Method string
ProxyUrl string //代理URL
PostData url.Values
Body io.Reader
Timeout int //超时时间,秒
}
func NewHttpx(reqUrl string) (h *Httpx) {
headers := make(map[string]string)
headers["User-Agent"] = headerUserAgent
return &Httpx{
Url: reqUrl,
Headers: headers,
Method: "GET",
Timeout: 30,
}
}
//获取最后一次的请求时间
func GetLastRequestTime() time.Time {
return last_request_time
}
//添加header
func (h *Httpx) AddHeader(key, value string) {
h.Headers[key] = value
}
//添加cookie
func (h *Httpx) AddCookie(c *http.Cookie) {
h.Cookies = append(h.Cookies, c)
}
//添加POST值
func (h *Httpx) AddPostValue(key string, values []string) {
if h.PostData == nil {
h.PostData = make(url.Values)
}
if values != nil {
for _, v := range values {
h.PostData.Add(key, v)
}
}
}
// 发送请求【请求失败后重复请求】
// int loopCount 重复请求次数
// int interval 重复请求的间隔,秒
func (h *Httpx) SendLoop(loopCount, interval int) (response *http.Response, err error) {
reqIndex := 0 //当前请求数
ST:
reqIndex++
response, err = h.Send()
if err != nil {
if reqIndex < loopCount {
time.Sleep(time.Second * time.Duration(interval))
goto ST
}
}
return response, err
}
// 发送请求【请求失败、状态码不为200时 重复请求】
// int loopCount 重复请求次数
// int interval 重复请求的间隔,秒
func (h *Httpx) SendLoopStatusCodeMustIsOK(loopCount, interval int) (response *http.Response, err error) {
reqIndex := 0 //当前请求数
ST:
reqIndex++
response, err = h.Send()
if err != nil {
if reqIndex < loopCount {
time.Sleep(time.Second * time.Duration(interval))
goto ST
}
} else {
if response.StatusCode != 200 {
response.Body.Close()
err = errors.New("请求状态码为:" + strconv.Itoa(response.StatusCode))
if reqIndex < loopCount {
time.Sleep(time.Second * time.Duration(interval))
goto ST
}
}
}
return response, err
}
//发送请求
func (h *Httpx) Send() (response *http.Response, err error) {
if h.Url == "" {
return nil, errors.New("URL is empty")
}
defer func() {
if err != nil && h.ClientIP != "" {
err = errors.New(err.Error() + " client ip is " + h.ClientIP)
}
}()
var req *http.Request
if h.Method == "POST" {
if h.Body == nil {
req, _ = http.NewRequest("POST", h.Url, strings.NewReader(h.PostData.Encode()))
} else {
req, _ = http.NewRequest("POST", h.Url, h.Body)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
} else {
req, _ = http.NewRequest(h.Method, h.Url, nil)
}
//headers
if len(h.Headers) > 0 {
for k, v := range h.Headers {
req.Header.Set(k, v)
}
}
//cookies
if len(h.Cookies) > 0 {
for _, v := range h.Cookies {
req.AddCookie(v)
}
}
transport := &http.Transport{DisableKeepAlives: true, ResponseHeaderTimeout: time.Second * 20}
//是否使用代理
if h.ProxyUrl != "" {
proxy, err := url.Parse(h.ProxyUrl)
if err != nil {
return nil, err
}
transport.Proxy = http.ProxyURL(proxy)
}
//设置超时时间
dialer := net.Dialer{
Timeout: time.Duration(h.Timeout) * time.Second,
Deadline: time.Now().Add(time.Duration(h.Timeout) * time.Second),
}
//是否使用指定的IP发送请求
if h.ClientIP != "" {
transport.Dial = func(network, address string) (net.Conn, error) {
//本地地址 本地外网IP
lAddr, err := net.ResolveTCPAddr(network, h.ClientIP+":0")
if err != nil {
return nil, err
}
dialer.LocalAddr = lAddr
return dialer.Dial(network, address)
}
} else {
transport.Dial = func(network, address string) (net.Conn, error) {
return dialer.Dial(network, address)
}
}
client := &http.Client{
Transport: transport,
}
response, err = client.Do(req)
//记录最后一次请求完成的时间
last_request_time = time.Now()
return response, err
}
// GET请求
func HttpGet(reqUrl string) (*http.Response, error) {
hx := NewHttpx(reqUrl)
return hx.Send()
}
//利用指定的IP发送请求
func HttpGetFromIP(reqUrl, ipaddr string) (*http.Response, error) {
hx := NewHttpx(reqUrl)
hx.ClientIP = ipaddr
return hx.Send()
}
// http GET 代理
func HttpGetFromProxy(reqUrl, proxyURL string) (*http.Response, error) {
hx := NewHttpx(reqUrl)
hx.ProxyUrl = proxyURL
return hx.Send()
}
//POST请求
func HttpPost(reqUrl string, postValues map[string][]string) (*http.Response, error) {
hx := NewHttpx(reqUrl)
hx.Method = "POST"
if postValues != nil {
for k, v := range postValues {
hx.AddPostValue(k, v)
}
}
return hx.Send()
}