-
Notifications
You must be signed in to change notification settings - Fork 4
/
UtilHttp.go
68 lines (58 loc) · 1.25 KB
/
UtilHttp.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
package goghostex
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
func NewHttpRequest(
client *http.Client,
reqType,
reqUrl,
postData string,
reqHeaders map[string]string,
) ([]byte, error) {
var req *http.Request
if strings.ToUpper(reqType) == http.MethodGet {
req, _ = http.NewRequest(strings.ToUpper(reqType), reqUrl, nil)
} else {
req, _ = http.NewRequest(strings.ToUpper(reqType), reqUrl, strings.NewReader(postData))
}
if req.Header.Get("User-Agent") == "" {
req.Header.Set(
"User-Agent",
"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36",
)
}
if reqHeaders != nil {
for k, v := range reqHeaders {
req.Header.Add(k, v)
}
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
bodyData, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
successCode := map[int]string{
200: "成功",
201: "创建",
202: "已接受",
}
if _, exist := successCode[resp.StatusCode]; !exist {
return nil, errors.New(fmt.Sprintf(
"HttpStatusCode: %d, HttpMethod: %s, Response: %s, Request: %s, Url: %s",
resp.StatusCode,
reqType,
string(bodyData),
postData,
reqUrl,
))
}
return bodyData, nil
}