-
Notifications
You must be signed in to change notification settings - Fork 3
/
httpclient.go
70 lines (53 loc) · 1.33 KB
/
httpclient.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
package xmpush
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
)
func DoPost(uri string, appSecret string, form *url.Values) ([]byte, error) {
param := ""
if form != nil {
param = form.Encode()
}
//fmt.Println(strings.NewReader(param))
req, err := http.NewRequest("POST", fmt.Sprintf("%s", baseHost()+uri), strings.NewReader(param))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
req.Header.Add("Authorization", fmt.Sprintf("key=%s", appSecret))
var client = &http.Client{
Timeout: 5 * time.Second,
}
res, err := client.Do(req)
body, err := ioutil.ReadAll(res.Body)
res.Body.Close()
return body, nil
}
func DoGet(uri string, appSecret string, form *url.Values) ([]byte, error) {
param := ""
if form != nil {
param = form.Encode()
}
req, err := http.NewRequest("GET",
fmt.Sprintf("%s?%s", baseHost()+uri, param),
nil)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
req.Header.Add("Authorization", fmt.Sprintf("key=%s", appSecret))
var client = &http.Client{
Timeout: 5 * time.Second,
}
res, err := client.Do(req)
body, err := ioutil.ReadAll(res.Body)
res.Body.Close()
return body, nil
}
func baseHost() string {
return ProductionHost
}