-
Notifications
You must be signed in to change notification settings - Fork 1
/
api_request.go
57 lines (46 loc) · 1.13 KB
/
api_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
package netease
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
)
type ClientExtension interface {
ExtendRequest(n *NetEase, req *http.Request)
}
func (n *NetEase) Request(ext ClientExtension, action, path, data string) ([]byte, error) {
isPost := action == "POST"
url := fmt.Sprintf("%s%s", n.BaseURL, path)
var bodyReader *strings.Reader = nil
if isPost {
bodyReader = strings.NewReader(data)
}
req, err := http.NewRequest(action, url, bodyReader)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", n.UserAgent)
req.Header.Set("X-Real-IP", n.FakeIP)
req.Header.Set("Referer", n.BaseURL)
req.Header.Set("Accept-language", "zh-cn")
req.Header.Set("Accept", "*/*")
if isPost {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Content-Length", string(len(data)))
}
// Last chance to change anything...!
if ext != nil {
ext.ExtendRequest(n, req)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
result, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = resp.Body.Close()
return result, err
}