-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
executable file
·141 lines (124 loc) · 3.74 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
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
package wxweb
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/cookiejar"
"net/url"
"time"
)
type Client struct {
client *http.Client
userAgent string
}
type Header map[string]string
func NewClient() *Client {
var netTransport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{Timeout: 100 * time.Second}).Dial,
// TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
// TLSHandshakeTimeout: 100 * time.Second,
}
cookieJar, _ := cookiejar.New(nil)
httpClient := &http.Client{
Timeout: time.Second * 100,
Transport: netTransport,
Jar: cookieJar,
}
return &Client{
client: httpClient,
userAgent: "ApiV2 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36 ",
}
}
func (c *Client) SetJar(jar http.CookieJar) {
c.client.Jar = jar
}
func (c *Client) Get(url string, data *url.Values) ([]byte, error) {
if data != nil {
url = url + "?" + data.Encode()
}
return c.fetch("GET", url, []byte(""), Header{})
}
func (c *Client) GetByte(url string, data []byte) ([]byte, error) {
return c.fetch("GET", url, data, Header{})
}
func (c *Client) GetWithHeader(url string, heder Header) ([]byte, error) {
return c.fetch("GET", url, nil, heder)
}
func (c *Client) Post(url string, data *url.Values) ([]byte, error) {
return c.fetch("POST", url, []byte(data.Encode()), Header{"Content-Type": "application/x-www-form-urlencoded"})
}
func (c *Client) PostJson(url string, m map[string]interface{}) ([]byte, error) {
jsonString, err := json.Marshal(m)
if err != nil {
return nil, err
}
return c.fetch("POST", url, jsonString, Header{"Content-Type": "application/json; charset=UTF-8"})
}
func (c *Client) PostJsonByte(url string, json []byte) ([]byte, error) {
return c.fetch("POST", url, json, Header{"Content-Type": "application/json; charset=UTF-8"})
}
func (c *Client) PostJsonByteForResp(url string, json []byte) (*http.Response, []byte, error) {
return c.fetchResp("POST", url, json, Header{"Content-Type": "application/json; charset=UTF-8"})
}
func (c *Client) fetchReponse(method string, uri string, body []byte, headers map[string]string) (*http.Response, error) {
req, err := http.NewRequest(method, uri, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", c.userAgent)
for k, v := range headers {
req.Header.Set(k, v)
}
return c.client.Do(req)
}
func (c *Client) fetchReponseWithReader(method string, uri string, body io.Reader, headers map[string]string) (*http.Response, error) {
req, err := http.NewRequest(method, uri, body)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", c.userAgent)
for k, v := range headers {
req.Header.Set(k, v)
}
return c.client.Do(req)
}
func (c *Client) fetchWithReader(method string, uri string, body io.Reader, headers Header) ([]byte, error) {
resp, err := c.fetchReponseWithReader(method, uri, body, headers)
if err != nil {
return nil, err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return b, nil
}
func (c *Client) fetch(method string, uri string, body []byte, headers Header) ([]byte, error) {
resp, err := c.fetchReponse(method, uri, body, headers)
if err != nil {
return nil, err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return b, nil
}
func (c *Client) fetchResp(method string, uri string, body []byte, headers Header) (resp *http.Response, b []byte, err error) {
resp, err = c.fetchReponse(method, uri, body, headers)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
b, err2 := ioutil.ReadAll(resp.Body)
if err2 != nil {
return nil, nil, err2
}
return resp, b, nil
}