-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.go
192 lines (173 loc) · 5.02 KB
/
fetch.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
package requester
import (
"bytes"
"encoding/json"
"fmt"
"github.com/tickstep/library-go/requester/rio"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
// HttpGet 简单实现 http 访问 GET 请求
func HttpGet(urlStr string) (body []byte, err error) {
resp, err := DefaultClient.Get(urlStr)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
return ioutil.ReadAll(resp.Body)
}
// HttpPost 简单的HTTP POST方法
func HttpPost(urlStr string, postData interface{}) (body []byte, err error) {
return Fetch("POST", urlStr, postData, nil)
}
// Req 参见 *HTTPClient.Req, 使用默认 http 客户端
func Req(method string, urlStr string, post interface{}, header map[string]string) (resp *http.Response, err error) {
return DefaultClient.Req(method, urlStr, post, header)
}
// Fetch 参见 *HTTPClient.Fetch, 使用默认 http 客户端
func Fetch(method string, urlStr string, post interface{}, header map[string]string) (body []byte, err error) {
return DefaultClient.Fetch(method, urlStr, post, header)
}
// Req 实现 http/https 访问,
// 根据给定的 method (GET, POST, HEAD, PUT 等等), urlStr (网址),
// post (post 数据), header (header 请求头数据), 进行网站访问。
// 返回值分别为 *http.Response, 错误信息
func (h *HTTPClient) Req(method string, urlStr string, post interface{}, header map[string]string) (resp *http.Response, err error) {
h.lazyInit()
var (
req *http.Request
obody io.Reader
contentLength int64
contentType string
)
if post != nil {
isJson := false
if header != nil {
if ct, ok := header["Content-Type"]; ok {
if strings.Contains(strings.ToLower(ct), "application/json") {
isJson = true
}
}
if ct, ok := header["content-type"]; ok {
if strings.Contains(strings.ToLower(ct), "application/json") {
isJson = true
}
}
}
if isJson {
switch value := post.(type) {
case io.Reader:
obody = value
case map[string]string:
paramJson, _ := json.Marshal(value)
obody = strings.NewReader(string(paramJson))
case map[string]interface{}:
paramJson, _ := json.Marshal(value)
obody = strings.NewReader(string(paramJson))
case map[interface{}]interface{}:
paramJson, _ := json.Marshal(value)
obody = strings.NewReader(string(paramJson))
case string:
obody = strings.NewReader(value)
case []byte:
obody = bytes.NewReader(value[:])
default:
paramJson, _ := json.Marshal(value)
obody = strings.NewReader(string(paramJson))
}
} else {
switch value := post.(type) {
case io.Reader:
obody = value
case map[string]string:
query := url.Values{}
for k := range value {
query.Set(k, value[k])
}
obody = strings.NewReader(query.Encode())
case map[string]interface{}:
query := url.Values{}
for k := range value {
query.Set(k, fmt.Sprint(value[k]))
}
obody = strings.NewReader(query.Encode())
case map[interface{}]interface{}:
query := url.Values{}
for k := range value {
query.Set(fmt.Sprint(k), fmt.Sprint(value[k]))
}
obody = strings.NewReader(query.Encode())
case string:
obody = strings.NewReader(value)
case []byte:
obody = bytes.NewReader(value[:])
default:
return nil, fmt.Errorf("requester.Req: unknown post type: %s", value)
}
}
switch value := post.(type) {
case ContentLengther:
contentLength = value.ContentLength()
case rio.Lener:
contentLength = int64(value.Len())
case rio.Lener64:
contentLength = value.Len()
}
switch value := post.(type) {
case ContentTyper:
contentType = value.ContentType()
}
}
req, err = http.NewRequest(method, urlStr, obody)
if err != nil {
return nil, err
}
if req.ContentLength <= 0 && contentLength != 0 {
req.ContentLength = contentLength
}
// 设置浏览器标识
req.Header.Set("User-Agent", h.UserAgent)
// 设置Content-Type
if contentType != "" {
req.Header.Set("Content-Type", contentType)
}
if header != nil {
// 处理Host
if host, ok := header["Host"]; ok {
req.Host = host
}
for key := range header {
req.Header.Set(key, header[key])
}
}
return h.Client.Do(req)
}
// Fetch 实现 http/https 访问,
// 根据给定的 method (GET, POST, HEAD, PUT 等等), urlStr (网址),
// post (post 数据), header (header 请求头数据), 进行网站访问。
// 返回值分别为 网站主体, 错误信息
func (h *HTTPClient) Fetch(method string, urlStr string, post interface{}, header map[string]string) (body []byte, err error) {
h.lazyInit()
resp, err := h.Req(method, urlStr, post, header)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
return ioutil.ReadAll(resp.Body)
}
func (h *HTTPClient) DoGet(urlStr string) (body []byte, err error) {
return h.Fetch("GET", urlStr, nil, nil)
}
func (h *HTTPClient) DoPost(urlStr string, post interface{}) (body []byte, err error) {
headers := map[string]string{
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
}
return h.Fetch("POST", urlStr, post, headers)
}