@@ -2,21 +2,15 @@ package util
2
2
3
3
import (
4
4
"bytes"
5
- "crypto/tls"
6
5
"encoding/json"
7
- "encoding/pem"
8
- "encoding/xml"
9
6
"fmt"
10
7
logger "github.com/fideism/golang-wechat/log"
11
8
"github.com/sirupsen/logrus"
12
9
"io"
13
10
"io/ioutil"
14
- "log"
15
11
"mime/multipart"
16
12
"net/http"
17
13
"os"
18
-
19
- "golang.org/x/crypto/pkcs12"
20
14
)
21
15
22
16
//HTTPGet get 请求
@@ -185,94 +179,3 @@ func PostMultipartForm(fields []MultipartFormField, uri string) (respBody []byte
185
179
respBody , err = ioutil .ReadAll (resp .Body )
186
180
return
187
181
}
188
-
189
- //PostXML perform a HTTP/POST request with XML body
190
- func PostXML (uri string , obj interface {}) ([]byte , error ) {
191
- logger .Entry ().WithFields (logrus.Fields {
192
- "url" : uri ,
193
- "data" : obj ,
194
- }).Debug ("发起微信post-xml请求" )
195
-
196
- xmlData , err := xml .Marshal (obj )
197
-
198
- if err != nil {
199
- return nil , err
200
- }
201
-
202
- body := bytes .NewBuffer (xmlData )
203
- response , err := http .Post (uri , "application/xml;charset=utf-8" , body )
204
- if err != nil {
205
- return nil , err
206
- }
207
- defer response .Body .Close ()
208
-
209
- if response .StatusCode != http .StatusOK {
210
- return nil , fmt .Errorf ("http code error : uri=%v , statusCode=%v" , uri , response .StatusCode )
211
- }
212
- return ioutil .ReadAll (response .Body )
213
- }
214
-
215
- //httpWithTLS CA证书
216
- func httpWithTLS (rootCa , key string ) (* http.Client , error ) {
217
- var client * http.Client
218
- certData , err := ioutil .ReadFile (rootCa )
219
- if err != nil {
220
- return nil , fmt .Errorf ("unable to find cert path=%s, error=%v" , rootCa , err )
221
- }
222
- cert := pkcs12ToPem (certData , key )
223
- config := & tls.Config {
224
- Certificates : []tls.Certificate {cert },
225
- }
226
- tr := & http.Transport {
227
- TLSClientConfig : config ,
228
- DisableCompression : true ,
229
- }
230
- client = & http.Client {Transport : tr }
231
- return client , nil
232
- }
233
-
234
- //pkcs12ToPem 将Pkcs12转成Pem
235
- func pkcs12ToPem (p12 []byte , password string ) tls.Certificate {
236
- blocks , err := pkcs12 .ToPEM (p12 , password )
237
- defer func () {
238
- if x := recover (); x != nil {
239
- log .Print (x )
240
- }
241
- }()
242
- if err != nil {
243
- panic (err )
244
- }
245
- var pemData []byte
246
- for _ , b := range blocks {
247
- pemData = append (pemData , pem .EncodeToMemory (b )... )
248
- }
249
- cert , err := tls .X509KeyPair (pemData , pemData )
250
- if err != nil {
251
- panic (err )
252
- }
253
- return cert
254
- }
255
-
256
- //PostXMLWithTLS perform a HTTP/POST request with XML body and TLS
257
- func PostXMLWithTLS (uri string , obj interface {}, ca , key string ) ([]byte , error ) {
258
- xmlData , err := xml .Marshal (obj )
259
- if err != nil {
260
- return nil , err
261
- }
262
-
263
- body := bytes .NewBuffer (xmlData )
264
- client , err := httpWithTLS (ca , key )
265
- if err != nil {
266
- return nil , err
267
- }
268
- response , err := client .Post (uri , "application/xml;charset=utf-8" , body )
269
- if err != nil {
270
- return nil , err
271
- }
272
- defer response .Body .Close ()
273
-
274
- if response .StatusCode != http .StatusOK {
275
- return nil , fmt .Errorf ("http code error : uri=%v , statusCode=%v" , uri , response .StatusCode )
276
- }
277
- return ioutil .ReadAll (response .Body )
278
- }
0 commit comments