1
0
mirror of https://github.com/silenceper/wechat.git synced 2026-02-23 13:42:25 +08:00
This commit is contained in:
oscar
2023-12-29 01:33:40 +00:00
committed by GitHub
3 changed files with 24 additions and 13 deletions

View File

@@ -10,7 +10,7 @@ jobs:
golangci: golangci:
strategy: strategy:
matrix: matrix:
go-version: [ '1.16','1.17','1.18','1.19','1.20','1.21' ] go-version: [ '1.16','1.17','1.18','1.19','1.20','1.21.4' ]
name: golangci-lint name: golangci-lint
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:

View File

@@ -7,13 +7,14 @@ import (
"encoding/json" "encoding/json"
"encoding/pem" "encoding/pem"
"encoding/xml" "encoding/xml"
"fmt"
"io" "io"
"log" "log"
"mime/multipart" "mime/multipart"
"net/http" "net/http"
"os" "os"
"fmt"
"golang.org/x/crypto/pkcs12" "golang.org/x/crypto/pkcs12"
) )
@@ -22,6 +23,9 @@ type URIModifier func(uri string) string
var uriModifier URIModifier var uriModifier URIModifier
// DefaultHTTPClient 默认httpClient
var DefaultHTTPClient = http.DefaultClient
// SetURIModifier 设置URI修改器 // SetURIModifier 设置URI修改器
func SetURIModifier(fn URIModifier) { func SetURIModifier(fn URIModifier) {
uriModifier = fn uriModifier = fn
@@ -41,7 +45,7 @@ func HTTPGetContext(ctx context.Context, uri string) ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
response, err := http.DefaultClient.Do(request) response, err := DefaultHTTPClient.Do(request)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -73,7 +77,7 @@ func HTTPPostContext(ctx context.Context, uri string, data []byte, header map[st
request.Header.Set(key, value) request.Header.Set(key, value)
} }
response, err := http.DefaultClient.Do(request) response, err := DefaultHTTPClient.Do(request)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -102,7 +106,7 @@ func PostJSONContext(ctx context.Context, uri string, obj interface{}) ([]byte,
return nil, err return nil, err
} }
req.Header.Set("Content-Type", "application/json;charset=utf-8") req.Header.Set("Content-Type", "application/json;charset=utf-8")
response, err := http.DefaultClient.Do(req) response, err := DefaultHTTPClient.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -129,7 +133,7 @@ func PostJSONWithRespContentType(uri string, obj interface{}) ([]byte, string, e
return nil, "", err return nil, "", err
} }
response, err := http.Post(uri, "application/json;charset=utf-8", jsonBuf) response, err := DefaultHTTPClient.Post(uri, "application/json;charset=utf-8", jsonBuf)
if err != nil { if err != nil {
return nil, "", err return nil, "", err
} }
@@ -205,7 +209,7 @@ func PostMultipartForm(fields []MultipartFormField, uri string) (respBody []byte
contentType := bodyWriter.FormDataContentType() contentType := bodyWriter.FormDataContentType()
bodyWriter.Close() bodyWriter.Close()
resp, e := http.Post(uri, contentType, bodyBuf) resp, e := DefaultHTTPClient.Post(uri, contentType, bodyBuf)
if e != nil { if e != nil {
err = e err = e
return return
@@ -229,7 +233,7 @@ func PostXML(uri string, obj interface{}) ([]byte, error) {
} }
body := bytes.NewBuffer(xmlData) body := bytes.NewBuffer(xmlData)
response, err := http.Post(uri, "application/xml;charset=utf-8", body) response, err := DefaultHTTPClient.Post(uri, "application/xml;charset=utf-8", body)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -252,11 +256,10 @@ func httpWithTLS(rootCa, key string) (*http.Client, error) {
config := &tls.Config{ config := &tls.Config{
Certificates: []tls.Certificate{cert}, Certificates: []tls.Certificate{cert},
} }
tr := &http.Transport{ trans := (DefaultHTTPClient.Transport.(*http.Transport)).Clone()
TLSClientConfig: config, trans.TLSClientConfig = config
DisableCompression: true, trans.DisableCompression = true
} client = &http.Client{Transport: trans}
client = &http.Client{Transport: tr}
return client, nil return client, nil
} }

View File

@@ -1,8 +1,11 @@
package wechat package wechat
import ( import (
"net/http"
"os" "os"
"github.com/silenceper/wechat/v2/util"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/silenceper/wechat/v2/cache" "github.com/silenceper/wechat/v2/cache"
@@ -81,3 +84,8 @@ func (wc *Wechat) GetWork(cfg *workConfig.Config) *work.Work {
} }
return work.NewWork(cfg) return work.NewWork(cfg)
} }
// SetHTTPClient 设置HTTPClient
func (wc *Wechat) SetHTTPClient(client *http.Client) {
util.DefaultHTTPClient = client
}