-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrequest.go
373 lines (344 loc) · 9.96 KB
/
request.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package skype
import (
"fmt"
"github.com/PuerkitoBio/goquery"
"github.com/gogf/gf/encoding/gurl"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"time"
)
/**
* get and post curl function
*/
type Request struct {
timeout time.Duration
}
/**
get login info, if not login, return login prompt , else return user login sign or token
*/
func (req *Request) getAuthorization() (sign string, dateTime string, err error) {
//
return
}
func (req *Request) requestReturnResponse(method string, reqUrl string, reqBody io.Reader, cookies map[string]string, header map[string]string) (response *http.Response, err error) {
u, err := gurl.ParseURL(reqUrl, 2)
if err != nil {
fmt.Println(err)
return
}
defaultDomain := u["host"]
//获得每次登录的信息 然后通过token 请求 skype 的官方接口
//默认超时
if req.timeout == 0 {
req.timeout = 10
}
client := &http.Client{
Timeout: req.timeout * time.Second, //set timeout
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
req1, err := http.NewRequest(method, reqUrl, reqBody) //set body
if err != nil {
return
}
agent := "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36"
//add commom header
req1.Header.Set("Accept", "*/*")
req1.Header.Set("Host", defaultDomain)
req1.Header.Set("Content-Type", "application/json")
req1.Header.Set("User-Agent", agent)
for k, v := range header {
req1.Header.Set(k, v)
}
if strings.Index(reqUrl, "ppsecure/post") > -1 {
// add other cookie
MaxAge := time.Hour * 24 / time.Second
if len(cookies) > 0 {
var newCookies []*http.Cookie
jar, _ := cookiejar.New(nil)
for cK, cV := range cookies {
newCookies = append(newCookies, &http.Cookie{
Name: cK,
Value: cV,
Path: "/",
Domain: defaultDomain,
MaxAge: int(MaxAge),
HttpOnly: false,
})
}
jar.SetCookies(req1.URL, newCookies)
client.Jar = jar
}
}
response, err = client.Do(req1)
if err != nil {
fmt.Println(err)
return
}
return response, nil
}
/**
底层的请求封装
*/
func (req *Request) request(method string, reqUrl string, reqBody io.Reader, cookies map[string]string, header map[string]string) (body string, err error, status int) {
resp, err := req.requestReturnResponse(method, reqUrl, reqBody, cookies, header)
if err != nil {
return
}
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
if resp.StatusCode == 302 {
location := resp.Header.Get("Location")
body = location
} else {
body = string(content)
fmt.Println(resp.Header)
}
status = resp.StatusCode
return
}
/**
* request can return headers
*/
func (req *Request) requestWithCookies(method string, reqUrl string, reqBody io.Reader, cookies map[string]string) (body string, err error, response *http.Response) {
resp, err := req.requestReturnResponse(method, reqUrl, reqBody, cookies, nil)
if err != nil {
return
}
//判断是否跳转
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
if resp.StatusCode == 302 {
location := resp.Header.Get("Location")
body = location
} else {
body = string(content)
}
response = resp
return
}
func (req *Request) requestWithCookiesReturnIdValue(method string, reqUrl string, reqBody io.Reader, cookies map[string]string, id string, selector string) (body string, err error, r io.Reader, tValue string) {
u, err := gurl.ParseURL(reqUrl, 2)
if err != nil {
fmt.Println(err)
return
}
defaultDomain := u["host"]
//获得每次登录的信息 然后通过token 请求 skype 的官方接口
sign, dateTime, err := req.getAuthorization()
if err != nil {
return
}
//默认超时
if req.timeout == 0 {
req.timeout = 10
}
client := &http.Client{
Timeout: req.timeout * time.Second, //set timeout
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
req1, err := http.NewRequest(method, reqUrl, reqBody) //set body
if err != nil {
return
}
agent := "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36"
//add commom header
req1.Header.Set("Accept", "*/*")
req1.Header.Set("Accept-Charset", "utf-8;")
req1.Header.Set("Host", defaultDomain)
req1.Header.Set("X-Date", dateTime)
req1.Header.Set("Content-Type", "application/html")
req1.Header.Set("Authorization", sign)
req1.Header.Set("User-Agent", agent)
//add other cookie
MaxAge := time.Hour * 24 / time.Second
if len(cookies) > 0 {
var newCookies []*http.Cookie
jar, _ := cookiejar.New(nil)
for cK, cV := range cookies {
newCookies = append(newCookies, &http.Cookie{
Name: cK,
Value: cV,
Path: "/",
Domain: defaultDomain,
MaxAge: int(MaxAge),
HttpOnly: false,
})
}
jar.SetCookies(req1.URL, newCookies)
client.Jar = jar
}
resp, err := client.Do(req1)
if err != nil {
return
}
//判断是否跳转
defer resp.Body.Close()
r = resp.Body
if id != "" && selector != "" {
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
log.Fatal(err)
}
doc.Find(selector).Each(func(_ int, s *goquery.Selection) {
idt, _ := s.Attr("id")
if idt == id {
tValue, _ = s.Attr("value")
}
})
}
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("resp.StatusCode", resp.StatusCode)
if resp.StatusCode == 302 {
location := resp.Header.Get("Location")
body = location
} else {
body = string(content)
}
return
}
/**
底层的请求封装
*/
func (req *Request) requestWithLogininfo(method string, reqUrl string, reqBody io.Reader, header map[string]string) (body string, err error, status int, skypetken, expires_in string) {
resp, err := req.requestReturnResponse(method, reqUrl, reqBody, nil, header)
if err != nil {
fmt.Println(err)
return
}
//获取登录信息值
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
log.Fatal(err)
}
// Find the review items
doc.Find("input").Each(func(i int, s *goquery.Selection) {
// For each item found, get the band and title
attrName, _ := s.Attr("name")
attrVlue, _ := s.Attr("value")
if attrName == "skypetoken" {
skypetken = attrVlue
}
if attrName == "expires_in" {
expires_in = attrVlue
}
})
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
if resp.StatusCode == 302 {
location := resp.Header.Get("Location")
body = location
} else {
body = string(content)
}
status = resp.StatusCode
return
}
/**
GET function
*/
func (req *Request) HttpGetJson(path string, params url.Values) (body string, err error, http_status int) {
//组装
reqUrl := fmt.Sprintf("%s?%s", path, gurl.BuildQuery(params))
//请求
body, err, http_status = req.request("GET", reqUrl, nil, nil, nil)
return
}
func (req *Request) HttpPostBase(path string, reqBody io.Reader, header map[string]string) (body string, err error, http_code int, skype_token, expires_in string) {
body, err, http_code, skype_token, expires_in = req.requestWithLogininfo("POST", path, reqBody, header)
return
}
func (req *Request) HttpGetJsonBackResponse(path string, params url.Values) (body string, err error, res *http.Response) {
//组装
reqUrl := fmt.Sprintf("%s?%s", path, gurl.BuildQuery(params))
//请求
body, err, res = req.requestWithCookies("GET", reqUrl, nil, nil)
return
}
/**
POST
@params example
gin.H = type H map[string]interface{}
searchCon := gin.H{
"pageNum": page,
"pageSize": limit,
"data": search,
}
params, _ := json.Marshal(searchCon)
*/
func (req *Request) HttpPostJson(path string, params string, cookies map[string]string) (body string, err error, res *http.Response) {
//请求
body, err, res = req.requestWithCookies("POST", path, strings.NewReader(params), cookies)
return
}
/**
add post request with params and data
*/
func (req *Request) HttpPostWithParamAndDataWithIdt(path string, params url.Values, data string, cookies map[string]string, id string) (body string, err error, res io.Reader, tValue string) {
reqUrl := fmt.Sprintf("%s?%s", path, gurl.BuildQuery(params))
body, err, res, tValue = req.requestWithCookiesReturnIdValue("POST", reqUrl, strings.NewReader(data), cookies, id, "input")
return
}
func (req *Request) HttpPostRegistrationToken(path string, data string, header map[string]string) (registrationToken, location string, err error) {
//获得 resgistration token 信息
resp, err := req.requestReturnResponse("POST", path, strings.NewReader(data), nil, header)
if err != nil {
return
}
defer resp.Body.Close()
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
return
}
registrationToken = resp.Header.Get("Set-Registrationtoken")
location = resp.Header.Get("Location")
return
}
func (req *Request) HttpGetWitHeaderAndCookiesJson(path string, params url.Values, data string, cookies map[string]string, headers map[string]string) (body string, err error) {
reqUrl := path
if len(params) >0 {
reqUrl = fmt.Sprintf("%s?%s", path, gurl.BuildQuery(params))
}
body, err, _ = req.request("GET", reqUrl, nil, cookies, headers)
return
}
func (req *Request) HttpPostWitHeaderAndCookiesJson(path string, params url.Values, data string, cookies map[string]string, headers map[string]string) (body string, err error) {
reqUrl := path
if len(params) >0 {
reqUrl = fmt.Sprintf("%s?%s", path, gurl.BuildQuery(params))
}
body, err, _ = req.request("POST", reqUrl, strings.NewReader(data), cookies, headers)
return
}
func (req *Request) HttpPutWitHeaderAndCookiesJson (path string, params url.Values, data string, cookies map[string]string, headers map[string]string) (body string, httpCode int, err error){
reqUrl := path
if len(params) >0 {
reqUrl = fmt.Sprintf("%s?%s", path, gurl.BuildQuery(params))
}
body, err, httpCode = req.request("PUT", reqUrl, strings.NewReader(data), cookies, headers)
return
}