/
HttpClient.go
343 lines (311 loc) · 8.28 KB
/
HttpClient.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package net
import (
"bufio"
"bytes"
"github.com/abeir/desktop-app/core"
"github.com/abeir/desktop-app/core/log"
"io"
"io/ioutil"
"mime/multipart"
"net"
"net/http"
"os"
"path/filepath"
"sync"
"time"
)
// HttpMethod http请求类型,post、get等等
type HttpMethod string
func (h HttpMethod) ToString() string{
return string(h)
}
const (
HttpGet HttpMethod = "GET"
HttpHead HttpMethod = "HEAD"
HttpPost HttpMethod = "POST"
HttpPut HttpMethod = "PUT"
HttpPatch HttpMethod = "PATCH"
HttpDelete HttpMethod = "DELETE"
HttpConnect HttpMethod = "CONNECT"
HttpOptions HttpMethod = "OPTIONS"
HttpTrace HttpMethod = "TRACE"
)
// ContentType 请求头Content-Type的值
type ContentType string
func (c ContentType) ToString() string{
return string(c)
}
const (
FormUrlencoded ContentType = "application/x-www-form-urlencoded"
FormData ContentType = "multipart/form-data"
Json ContentType = "application/json"
)
var client *http.Client
// NewHttpClient 创建HttpClient实例
// HttpClient中的Set、Add方法支持链式调用方式,最后通过Reqeust方法发送请求
// 默认情况下,会将Content-Type设置为application/x-www-form-urlencoded
func NewHttpClient() *HttpClient{
if client==nil {
var lock = sync.Mutex{}
lock.Lock()
defer lock.Unlock()
if client==nil {
var transport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
client = &http.Client{
Transport: transport,
Timeout: time.Second * 15,
}
}
}
httpClient := &HttpClient{
client: client,
method: HttpGet,
headers: make(map[string]string),
rspHeaders: make(map[string][]string),
}
httpClient.SetContentType(FormUrlencoded)
return httpClient
}
// HttpClient http客户端,不要手动构建该实例,应该调用NewHttpClient函数创建实例
type HttpClient struct {
client *http.Client
method HttpMethod
headers map[string]string
cookies []http.Cookie
body io.Reader
rspHeaders map[string][]string
err error
}
// AddHeader 添加请求头
func (h *HttpClient) AddHeader(name, value string) *HttpClient{
h.headers[name] = value
return h
}
// AddHeaders 批量添加请求头
func (h *HttpClient) AddHeaders(headers map[string]string) *HttpClient{
for k,v := range headers {
h.headers[k] = v
}
return h
}
func (h *HttpClient) AddCookie(cookie *http.Cookie) *HttpClient{
h.cookies = append(h.cookies, *cookie)
return h
}
// SetContentType 设置Content-Type
func (h *HttpClient) SetContentType(contentType ContentType) *HttpClient{
h.headers["Content-Type"] = contentType.ToString()
return h
}
// SetUserAgent 设置User-Agent
func (h *HttpClient) SetUserAgent(userAgent string) *HttpClient{
h.headers["User-Agent"] = userAgent
return h
}
// SetMethod 设置请求方法,GET、POST、DELETE等
func (h *HttpClient) SetMethod(method HttpMethod) *HttpClient{
h.method = method
return h
}
// SetBody 设置请求体内容
func (h *HttpClient) SetBody(body []byte) *HttpClient{
h.body = bytes.NewBuffer(body)
return h
}
// SetBodyStream 设置请求体内容
func (h *HttpClient) SetBodyStream(body io.Reader) *HttpClient{
h.body = body
return h
}
// SetBodyMap 设置请求体内容,通常post请求参数可以放置在此处
func (h *HttpClient) SetBodyMap(body map[string][]string) *HttpClient{
bodyBytes := h.bodyMap2bytes(body)
if len(bodyBytes)==0 {
return h
}
h.SetBody(bodyBytes)
return h
}
func (h *HttpClient) bodyMap2bytes(body map[string][]string) []byte{
if body==nil || len(body)==0 {
return []byte{}
}
var bodyBuff bytes.Buffer
for name,values := range body {
if values==nil {
bodyBuff.WriteString(name)
bodyBuff.WriteString("=")
bodyBuff.WriteString("&")
continue
}
for _,val := range values {
bodyBuff.WriteString(name)
bodyBuff.WriteString("=")
bodyBuff.WriteString(val)
bodyBuff.WriteString("&")
}
}
if bodyBuff.Len() == 0 {
return []byte{}
}
bodyBytes := bodyBuff.Bytes()
if bodyBytes[len(bodyBytes)-1] == '&' {
bodyBytes = bodyBytes[:len(bodyBytes)-1]
}
return bodyBytes
}
// MultipartForm 用于发送multipart/form-data类型的数据,会将Content-Type设置为multipart/form-data
// params: 请求参数
// files: 上传文件
func (h *HttpClient) MultipartForm(params map[string][]string, files map[string]string) *HttpClient{
var bodyReader bytes.Buffer
bodyWriter := multipart.NewWriter(&bodyReader)
if params!=nil {
for name,values := range params {
if values==nil || len(values)==0 {
if err := bodyWriter.WriteField(name, ""); err!=nil {
h.err = err
log.Error(err)
return h
}
continue
}
for _, val := range values {
if err := bodyWriter.WriteField(name, val); err!=nil {
h.err = err
log.Error(err)
return h
}
}
}
}
if files!=nil {
for name,file := range files {
filename := filepath.Base(file)
fileWriter, err := bodyWriter.CreateFormFile(name, filename)
if err!=nil {
h.err = err
log.Error(err)
return h
}
if err = h.copy(file, fileWriter); err!=nil {
h.err = err
log.Error(err)
return h
}
}
}
if err := bodyWriter.Close(); err!=nil {
h.err = err
log.Error(err)
return h
}
h.SetContentType(ContentType(bodyWriter.FormDataContentType()))
h.SetBodyStream(&bodyReader)
return h
}
func (h *HttpClient) copy(srcFile string, dst io.Writer) error{
f, _ := os.Open(srcFile)
defer core.CloseQuietly(f)
if _, err := io.Copy(dst, f); err!=nil {
return err
}
return nil
}
func (h *HttpClient) doRequest(url string) (rsp *http.Response, err error){
if h.err!=nil {
return nil, h.err
}
req, err := http.NewRequest(h.method.ToString(), url, h.body)
if err!=nil {
return nil, err
}
for k,v := range h.headers {
req.Header.Add(k, v)
}
for _, cookie := range h.cookies {
req.AddCookie(&cookie)
}
return h.client.Do(req)
}
func (h *HttpClient) extractRspHeaders(rsp *http.Response){
for k,v := range rsp.Header {
h.rspHeaders[k] = v
}
}
// Request 发送请求,调用成功后可使用 ResponseHeaders方法获取响应头
// url: 请求地址
// return
// body: 响应内容
// err: 请求过程中出现的错误
func (h *HttpClient) Request(url string) (body []byte, err error){
rsp, err := h.doRequest(url)
if err!=nil {
return nil, err
}
h.extractRspHeaders(rsp)
defer core.CloseQuietly(rsp.Body)
body, err = ioutil.ReadAll(rsp.Body)
return body, err
}
// RequestStream 发送请求,调用成功后可使用 ResponseHeaders方法获取响应头
// url: 请求地址
// return
// body: 响应内容
// err: 请求过程中出现的错误
func (h *HttpClient) RequestStream(url string) (body io.Writer, err error){
rsp, err := h.doRequest(url)
if err!=nil {
return nil, err
}
h.extractRspHeaders(rsp)
defer core.CloseQuietly(rsp.Body)
var bodyWriter bufio.Writer
_, err = io.Copy(&bodyWriter, rsp.Body)
return body, err
}
// ResponseHeaders 请求完成后,使用此方法获取响应头
func (h *HttpClient) ResponseHeaders() map[string][]string{
return h.rspHeaders
}
// FastGet 发送简单的GET请求,注意,调用该方法发送请求后 ResponseHeaders方法不会获取响应头
// url: 请求地址
// return
// body: 响应内容
// err: 请求过程中出现的错误
func (h *HttpClient) FastGet(url string) (body []byte, err error){
rsp, err := h.client.Get(url)
if err!=nil {
return nil, err
}
defer core.CloseQuietly(rsp.Body)
body, err = ioutil.ReadAll(rsp.Body)
return body, err
}
// FastPost 发送简单的POST请求,注意,调用该方法发送请求后 ResponseHeaders方法不会获取响应头
// url: 请求地址
// data: post请求参数
// return
// body: 响应内容
// err: 请求过程中出现的错误
func (h *HttpClient) FastPost(url string, data map[string][]string) (body []byte, err error){
requestBody := h.bodyMap2bytes(data)
rsp, err := h.client.Post(url, FormUrlencoded.ToString(), bytes.NewBuffer(requestBody))
if err!=nil {
return nil, err
}
defer core.CloseQuietly(rsp.Body)
body, err = ioutil.ReadAll(rsp.Body)
return body, err
}