/
http.go
205 lines (194 loc) · 6.79 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Copyright © 2021 - 2024 vity <vityme@icloud.com>.
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file.
*/
package x
import (
"bytes"
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
)
// HttpGet 执行Get请求
func HttpGet(requestUrl string) (bool, string, int) {
req, err := http.NewRequest("GET", requestUrl, nil)
if err != nil {
return false, fmt.Sprintf("[%s] %s", "x.HttpGet", "Network Create Error:"+err.Error()), 0
}
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
return false, fmt.Sprintf("[%s] %s", "x.HttpGet", "Network Request Error:"+err.Error()), 0
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return false, fmt.Sprintf("[%s] %s", "x.HttpGet", "Network Response Error:"+err.Error()), resp.StatusCode
}
return true, string(body), resp.StatusCode
}
// HttpPostForm 执行Post Form 请求
func HttpPostForm(requestUrl string, data url.Values) (bool, string, int) {
form := io.NopCloser(strings.NewReader(data.Encode()))
req, err := http.NewRequest("POST", requestUrl, form)
if err != nil {
return false, fmt.Sprintf("[%s] %s", "x.HttpPostForm", "Network Create Error:"+err.Error()), 0
}
client := http.Client{}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
resp, err := client.Do(req)
if err != nil {
return false, fmt.Sprintf("[%s] %s", "x.HttpPostForm", "Network Request Error:"+err.Error()), 0
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return false, fmt.Sprintf("[%s] %s", "x.HttpPostForm", "Network Response Error:"+err.Error()), 0
}
return true, string(body), resp.StatusCode
//client := http.Client{}
//resp, err := client.PostForm(requestUrl, data)
//if err != nil {
// return false, fmt.Sprintf("[%s] %s", "x.HttpPostForm", "Network Request Error:"+err.Error()), 0
//}
//defer func(Body io.ReadCloser) {
// _ = Body.Close()
//}(resp.Body)
//body, err := io.ReadAll(resp.Body)
//if err != nil {
// return false, fmt.Sprintf("[%s] %s", "x.HttpPostForm", "Network Response Error:"+err.Error()), 0
//}
//return true, string(body), resp.StatusCode
}
// HttpPostJson 执行Post JSON请求
func HttpPostJson(requestUrl string, json string) (bool, string, int) {
req, err := http.NewRequest("POST", requestUrl, bytes.NewBuffer([]byte(json)))
if err != nil {
return false, fmt.Sprintf("[%s] %s", "x.HttpPostJson", "Network Create Error:"+err.Error()), 0
}
client := http.Client{}
req.Header.Set("Content-Type", "application/json;charset=utf-8")
resp, err := client.Do(req)
if err != nil {
return false, fmt.Sprintf("[%s] %s", "x.HttpPostJson", "Network Request Error:"+err.Error()), 0
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return false, fmt.Sprintf("[%s] %s", "x.HttpPostJson", "Network Response Error:"+err.Error()), 0
}
return true, string(body), resp.StatusCode
}
// HttpPostJsonWithHeader 执行Post JSON请求
func HttpPostJsonWithHeader(requestUrl string, json string, headers map[string]string) (bool, string, int) {
req, err := http.NewRequest("POST", requestUrl, bytes.NewBuffer([]byte(json)))
if err != nil {
return false, fmt.Sprintf("[%s] %s", "x.HttpPostJsonWithHeader", "Network Create Error:"+err.Error()), 0
}
client := http.Client{}
req.Header.Set("Content-Type", "application/json")
for key, val := range headers {
req.Header.Set(key, val)
}
resp, err := client.Do(req)
if err != nil {
return false, fmt.Sprintf("[%s] %s", "x.HttpPostJsonWithHeader", "Network Request Error:"+err.Error()), 0
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return false, fmt.Sprintf("[%s] %s", "x.HttpPostJsonWithHeader", "Network Response Error:"+err.Error()), 0
}
return true, string(body), resp.StatusCode
}
// HttpPostXmlSecure 执行Post XML 证书请求
func HttpPostXmlSecure(requestUrl string, xml string, certFile string, keyFile string, rootCaFile string) (bool, string, int) {
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return false, fmt.Sprintf("[%s] %s", "x.HttpPostXmlSecure", "Init Cert & Key error:"+err.Error()), 0
}
root, err := os.ReadFile(rootCaFile)
if err != nil {
return false, fmt.Sprintf("[%s] %s", "x.HttpPostXmlSecure", "Init Rootca Cert error:"+err.Error()), 0
}
pool := x509.NewCertPool()
pool.AppendCertsFromPEM(root)
conf := &tls.Config{
Certificates: []tls.Certificate{cert},
}
c := &http.Client{Transport: &http.Transport{TLSClientConfig: conf}}
req, err := http.NewRequest(http.MethodPost, requestUrl, bytes.NewBuffer([]byte(xml)))
if err != nil {
return false, fmt.Sprintf("[%s] %s", "x.HttpPostXmlSecure", "Network Create Error:"+err.Error()), 0
}
req.Header.Set("Content-Type", "application/json")
resp, err := c.Do(req)
if err != nil {
return false, fmt.Sprintf("[%s] %s", "x.HttpPostXmlSecure", "Network Request Error:"+err.Error()), 0
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return false, fmt.Sprintf("[%s] %s", "x.HttpPostXmlSecure", "Network Response Error:"+err.Error()), 0
}
return true, string(body), resp.StatusCode
}
// HttpPostJsonDownload 执行Post JSON请求并获取响应文件
func HttpPostJsonDownload(requestUrl string, json string) (bool, string, *os.File, int) {
req, err := http.NewRequest("POST", requestUrl, bytes.NewBuffer([]byte(json)))
if err != nil {
return false, fmt.Sprintf("[%s] %s",
"x.HttpPostJsonDownload", "Network Create Error:"+err.Error(),
), nil, 0
}
client := http.Client{}
req.Header.Set("Content-Type", "application/json;charset=utf-8")
resp, err := client.Do(req)
if err != nil {
return false, fmt.Sprintf(
"[%s] %s", "x.HttpPostJsonDownload", "Network Request Error:"+err.Error(),
), nil, 0
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return false, fmt.Sprintf(
"[%s] %s", "x.HttpPostJsonDownload", "Network Response Error:"+err.Error(),
), nil, resp.StatusCode
}
if strings.HasPrefix(string(body), "{") && strings.HasSuffix(string(body), "}") {
return false, string(body), nil, resp.StatusCode
}
f, err := os.CreateTemp("", "")
if err != nil {
return false, fmt.Sprintf(
"[%s] %s", "x.HttpPostJsonDownload", "Create temp file Error:"+err.Error(),
), nil, 0
}
err = os.WriteFile(f.Name(), body, 0644)
if err != nil {
return false, fmt.Sprintf(
"[%s] %s", "x.HttpPostJsonDownload", "Write temp file Error:"+err.Error(),
), nil, resp.StatusCode
}
return true, "", f, resp.StatusCode
}