-
Notifications
You must be signed in to change notification settings - Fork 3
/
fetch.go
142 lines (126 loc) · 3.65 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
package requester
import (
"bytes"
"fmt"
"github.com/iikira/iikira-go-utils/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)
}
// 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 {
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())
contentType = "application/x-www-form-urlencoded"
case map[string]interface{}:
query := url.Values{}
for k := range value {
query.Set(k, fmt.Sprint(value[k]))
}
obody = strings.NewReader(query.Encode())
contentType = "application/x-www-form-urlencoded"
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())
contentType = "application/x-www-form-urlencoded"
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)
}