Score
0
Watch 30 Star 81 Fork 18

北京小程科技有限公司 / 微信Go SDKGoMIT

Join us
Explore and code with more than 2 million developers,Free private repositories !:)
Sign up
微信支付、公众号、小程序相关后端接口的Go语言实现。 spread retract

Clone or download
network.go 1.61 KB
Copy Edit Web IDE Raw Blame History
package wechat
import (
"bytes"
"encoding/json"
"io/ioutil"
"net"
"net/http"
"strings"
"time"
)
var client *http.Client
func init() {
client = &http.Client{
Timeout: 30 * time.Second,
Transport: &http.Transport{
IdleConnTimeout: 3 * time.Minute,
TLSHandshakeTimeout: 10 * time.Second,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 10 * time.Minute,
DualStack: true,
}).DialContext,
},
}
}
// 发送Get请求
func httpGet(url string) (body []byte, err error) {
resp, err := client.Get(url)
if err != nil {
return
}
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
return
}
// 发送Post请求
func httpPost(url string, body interface{}) (data []byte, err error) {
bodyStr, err := json.Marshal(body)
if err != nil {
return
}
resp, err := client.Post(url, "application/json", bytes.NewReader(bodyStr))
if err != nil {
return
}
defer resp.Body.Close()
data, err = ioutil.ReadAll(resp.Body)
return
}
// 发送Post请求,参数是XML格式的字符串
func httpPostXml(url string, xmlBody string) (body []byte, err error) {
resp, err := client.Post(url, "application/xml", strings.NewReader(xmlBody))
if err != nil {
return
}
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
return
}
// 发送带证书的Post请求,参数是XML格式的字符串
func httpPostXmlWithCert(url string, xmlBody string, client *http.Client) (body []byte, err error) {
resp, err := client.Post(url, "application/xml", strings.NewReader(xmlBody))
if err != nil {
return
}
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
return
}

Comment ( 0 )

Sign in for post a comment

Go
1
https://gitee.com/cuckoopark/wechat.git
git@gitee.com:cuckoopark/wechat.git
cuckoopark
wechat
微信Go SDK
master

Help Search

191139_cd20d5fd_5186603 191143_ebef6f8d_5186603