-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
125 lines (109 loc) · 2.81 KB
/
http.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
package myhttp
import (
"bytes"
"dannytools/ehand"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
)
// timeout millisecond
func RequestGet(url string, timeout uint32) ([]byte, error) {
var result []byte
tout := time.Duration(time.Duration(timeout) * time.Millisecond)
client := http.Client{Timeout: tout}
resp, err := client.Get(url)
if resp != nil {
if resp.Body != nil {
defer resp.Body.Close()
}
}
if err != nil {
if resp != nil {
resp.Close = true
if resp.Body != nil {
result, _ = ioutil.ReadAll(resp.Body)
}
}
return result, ehand.WithStackError(err)
}
result, err = ioutil.ReadAll(resp.Body)
resp.Close = true
if err != nil {
return result, ehand.WithStackError(err)
}
if resp.StatusCode != http.StatusOK {
return []byte{}, ehand.WithStackError(fmt.Errorf("request fail: errorcode: %d, errormsg:%s", resp.StatusCode, string(result)))
}
//fmt.Printf("request result : %s\n", result)
return result, nil
}
// timeout millisecond
func RequestPostJson(url string, timeout uint32, body []byte, header map[string]string) ([]byte, error, string) {
var result []byte
tout := time.Duration(time.Duration(timeout) * time.Millisecond)
client := http.Client{Timeout: tout}
bd := bytes.NewBuffer(body)
req, err := http.NewRequest("POST", url, bd)
if err != nil {
return result, ehand.WithStackError(err), err.Error()
}
for k, v := range header {
req.Header.Set(k, v)
}
resp, err := client.Do(req)
if resp != nil {
if resp.Body != nil {
defer resp.Body.Close()
}
}
if err != nil {
if resp != nil {
resp.Close = true
if resp.Body != nil {
result, _ = ioutil.ReadAll(resp.Body)
}
}
return result, ehand.WithStackError(err), err.Error()
}
result, err = ioutil.ReadAll(resp.Body)
resp.Close = true
if err != nil {
return result, ehand.WithStackError(err), err.Error()
}
if resp.StatusCode != http.StatusOK {
if len(result) > 0 {
return result, ehand.WithStackError(fmt.Errorf("request fail: errorcode: %d, errormsg:%s", resp.StatusCode, string(result))), string(result)
} else {
return nil, ehand.WithStackError(fmt.Errorf("request fail: errorcode: %d", resp.StatusCode)), ""
}
}
return result, nil, ""
}
func JoinUrlPaths(urlStr, relativePath string) (string, error) {
base, err := url.Parse(urlStr)
if err != nil {
return "", ehand.WithStackError(err)
}
rel, err := url.Parse(relativePath)
if err != nil {
return "", ehand.WithStackError(err)
}
return base.ResolveReference(rel).String(), nil
}
func BuildUrl(base string, params map[string]string) string {
if len(params) == 0 {
return base
}
p := ""
for k, v := range params {
if p == "" {
p = fmt.Sprintf("%s=%s", k, url.QueryEscape(v))
} else {
p = fmt.Sprintf("%s&%s=%s", p, k, url.QueryEscape(v))
}
}
//p = url.QueryEscape(p)
return fmt.Sprintf("%s?%s", base, p)
}