当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
1 Star 0 Fork 97

nil / 微信Go SDK
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
network.go 1.61 KB
一键复制 编辑 原始数据 按行查看 历史
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
}
Go
1
https://gitee.com/nlh1996/wechat.git
git@gitee.com:nlh1996/wechat.git
nlh1996
wechat
微信Go SDK
master

搜索帮助