-
Notifications
You must be signed in to change notification settings - Fork 1
/
request.go
103 lines (86 loc) · 1.92 KB
/
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
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
package main
import (
"crypto/tls"
"github.com/chucklqsun/glog"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
var _, _ = url.Parse("")
type aci_request struct {
account string
url string
proxy string
method string
head map[string]string
body string
cookie string
result interface{}
}
func (ar *aci_request) sendRequest() bool {
tr := &http.Transport{}
//ignore https verify
tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
//proxy setup,left empty if no use
if !strings.EqualFold(ar.proxy, "") {
proxy, _ := url.Parse(ar.proxy)
tr.Proxy = http.ProxyURL(proxy)
}
client := &http.Client{Transport: tr}
//get vendor method
method := ar.method
var (
req *http.Request
err error
body io.Reader //used for POST only
)
//setup request body
switch {
case method == "POST":
body = strings.NewReader(ar.body)
case method == "GET":
body = nil
default:
body = nil
}
req, err = http.NewRequest(method, ar.url, body)
//setup common header
for key, value := range common_head.data {
req.Header.Set(key, value)
}
req.Header.Set("cookie", ar.cookie)
//setup vendor header
//"head" convert interface to aci_head type
for key, value := range ar.head {
req.Header.Set(key, value)
}
resp, err := client.Do(req)
if err != nil {
glog.Errorln("Resp err:", err)
return false
}
defer resp.Body.Close()
respBody, err2 := ioutil.ReadAll(resp.Body)
if err2 != nil {
glog.Errorln("ReadAll err:", err)
return false
} else {
if feedback, err := json_decode(respBody); err == nil {
glog.Infoln(feedback)
if ret := ar.result.(func(map[string]interface{}) bool)(feedback); !ret {
glog.Infoln("exec ", ar.account, ar.url, " fail")
return false
} else {
glog.Infoln("exec ", ar.account, ar.url, " success")
return true
}
} else {
glog.Errorln("Json decode error", err)
glog.Errorln("body:", respBody)
return false
}
}
return false
}