forked from hxzqlh/GoEx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpUtils.go
160 lines (133 loc) · 4.41 KB
/
HttpUtils.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
package goex
//http request 工具函数
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
)
func DoHttpRequest(client *http.Client, reqType string, reqUrl string, postData []byte, requstHeaders map[string]string) ([]byte, error) {
req, _ := http.NewRequest(reqType, reqUrl, bytes.NewReader(postData))
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 requstHeaders != nil {
for k, v := range requstHeaders {
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
}
if resp.StatusCode != 200 {
return nil, errors.New(fmt.Sprintf("HttpStatusCode:%d ,Desc:%s", resp.StatusCode, string(bodyData)))
}
return bodyData, nil
}
func NewHttpRequest(client *http.Client, reqType string, reqUrl string, postData string, requstHeaders map[string]string) ([]byte, error) {
req, _ := http.NewRequest(reqType, reqUrl, strings.NewReader(postData))
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 requstHeaders != nil {
for k, v := range requstHeaders {
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
}
if resp.StatusCode != 200 {
return nil, errors.New(fmt.Sprintf("HttpStatusCode:%d ,Desc:%s", resp.StatusCode, string(bodyData)))
}
return bodyData, nil
}
func HttpGet(client *http.Client, reqUrl string) (map[string]interface{}, error) {
respData, err := NewHttpRequest(client, "GET", reqUrl, "", nil)
if err != nil {
return nil, err
}
var bodyDataMap map[string]interface{}
err = json.Unmarshal(respData, &bodyDataMap)
if err != nil {
log.Println(string(respData))
return nil, err
}
return bodyDataMap, nil
}
func HttpGet2(client *http.Client, reqUrl string, headers map[string]string) (map[string]interface{}, error) {
if headers == nil {
headers = map[string]string{}
}
headers["Content-Type"] = "application/x-www-form-urlencoded"
respData, err := NewHttpRequest(client, "GET", reqUrl, "", headers)
if err != nil {
return nil, err
}
var bodyDataMap map[string]interface{}
err = json.Unmarshal(respData, &bodyDataMap)
if err != nil {
log.Println("respData", string(respData))
return nil, err
}
return bodyDataMap, nil
}
func HttpGet3(client *http.Client, reqUrl string, headers map[string]string) ([]interface{}, error) {
if headers == nil {
headers = map[string]string{}
}
headers["Content-Type"] = "application/x-www-form-urlencoded"
respData, err := NewHttpRequest(client, "GET", reqUrl, "", headers)
if err != nil {
return nil, err
}
var bodyDataMap []interface{}
err = json.Unmarshal(respData, &bodyDataMap)
if err != nil {
log.Println("respData", string(respData))
return nil, err
}
return bodyDataMap, nil
}
func HttpPostForm(client *http.Client, reqUrl string, postData url.Values) ([]byte, error) {
headers := map[string]string{
"Content-Type": "application/x-www-form-urlencoded"}
return NewHttpRequest(client, "POST", reqUrl, postData.Encode(), headers)
}
func HttpPostForm2(client *http.Client, reqUrl string, postData url.Values, headers map[string]string) ([]byte, error) {
if headers == nil {
headers = map[string]string{}
}
headers["Content-Type"] = "application/x-www-form-urlencoded"
return NewHttpRequest(client, "POST", reqUrl, postData.Encode(), headers)
}
func HttpPostForm3(client *http.Client, reqUrl string, postData string, headers map[string]string) ([]byte, error) {
return NewHttpRequest(client, "POST", reqUrl, postData, headers)
}
func HttpPostForm4(client *http.Client, reqUrl string, postData []byte, headers map[string]string) ([]byte, error) {
if headers == nil {
headers = map[string]string{}
}
headers["Content-Type"] = "application/x-www-form-urlencoded"
return DoHttpRequest(client, "POST", reqUrl, postData, headers)
}
func HttpDeleteForm(client *http.Client, reqUrl string, postData url.Values, headers map[string]string) ([]byte, error) {
if headers == nil {
headers = map[string]string{}
}
headers["Content-Type"] = "application/x-www-form-urlencoded"
return NewHttpRequest(client, "DELETE", reqUrl, postData.Encode(), headers)
}