-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.go
85 lines (75 loc) · 1.76 KB
/
base.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
package easy_http
import (
"net/http"
"net/url"
"strings"
)
//异步回调的接口
type ICallBack interface {
EasyResponseCallback(IResponse)
}
//request重定向的回调函数
type CheckRedirect func(req *http.Request, via []*http.Request) error
type TlsPath struct {
//cert (pem) 路径
CertFile string
//key 路径
KeyFile string
}
//构造一个简单的HTTP请求cookie
func EasyCookie(simple map[string]string) []*http.Cookie {
if len(simple) == 0 {
return nil
}
cookies := make([]*http.Cookie, 0, len(simple))
for k, v := range simple {
cookies = append(cookies, &http.Cookie{
Name: k,
Value: v,
})
}
return cookies
}
//构造一个简单的GET请求协议
func EasyGet(strUrl string, values map[string]string) string {
if strUrl == "" || values == nil {
return strUrl
}
var buf strings.Builder
buf.WriteString(strUrl)
buf.WriteByte('?')
i := 0
for k, v := range values {
if i > 0 {
buf.WriteByte('&')
}
buf.WriteString(url.QueryEscape(k))
buf.WriteByte('=')
buf.WriteString(url.QueryEscape(v))
i++
}
return buf.String()
}
//构造一个简单的POST body,
func EasyPost(values map[string]string) url.Values {
if values == nil {
return nil
}
value := make(url.Values, len(values))
for k, v := range values {
value.Add(k, v)
}
return value
}
//POST请求中,处理request的函数
func EasyPostFromRequest(r *http.Request) {
r.Header.Set("Content-Type", HTTP_CONTENT_TYPE_FROM_DATA)
}
//POST请求中,处理request的函数,设置`Content-Type` 为 json
func EasyPostJsonRequest(r *http.Request) {
r.Header.Set("Content-Type", HTTP_CONTENT_TYPE_JSON)
}
//POST请求中,处理request的函数,设置`Content-Type` 为 xml
func EasyPostXmlRequest(r *http.Request) {
r.Header.Set("Content-Type", HTTP_CONTENT_TYPE_XML)
}