forked from nntaoli-project/goex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpUtils.go
67 lines (54 loc) · 1.7 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
package coinapi
//http request 工具函数
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
func _httpRequest(client *http.Client, reqType string, reqUrl string, postData url.Values, requstHeaders map[string]string) ([]byte, error) {
req, _ := http.NewRequest(reqType, reqUrl, strings.NewReader(postData.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
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
}
//var bodyDataMap map[string]interface{};
//err = json.Unmarshal(bodyData, &bodyDataMap);
//if err != nil {
// println(string(bodyData));
// return nil, err;
//}
return bodyData, nil
}
func HttpGet(client *http.Client, reqUrl string) (map[string]interface{}, error) {
respData, err := _httpRequest(client, "GET", reqUrl, url.Values{}, nil)
if err != nil {
return nil, err
}
var bodyDataMap map[string]interface{}
//fmt.Printf("\n%s\n", respData);
err = json.Unmarshal(respData, &bodyDataMap)
if err != nil {
return nil, err
}
return bodyDataMap, nil
}
func HttpPostForm(client *http.Client, reqUrl string, postData url.Values) ([]byte, error) {
return _httpRequest(client, "POST", reqUrl, postData, nil)
}
func HttpPostForm2(client *http.Client, reqUrl string, postData url.Values, headers map[string]string) ([]byte, error) {
return _httpRequest(client, "POST", reqUrl, postData, headers)
}