
本文共 2572 字,大约阅读时间需要 8 分钟。
������������������������������������������������������������Web������������������������������������������������������������������Golang������������������������HTTP���������������������������������������������������������������HTTPS POST���������
���������������������������HTTP���������
���Golang������������������������once.Do
������������������������������HTTP���������������������������������������������������CreateHTTPClient
������������������������������������HTTP������������������������������������������������������������
���������������������������������������������
package mainimport ( "github.com/cpuov/once" "github.com/go-libs/http" "github.com/go-libs/http/transport" "net/http" "time")var ( httpClient *http.Client once once.SyncOnce)func CreateHTTPClient() *http.Client { once.Do(func() { httpClient = &http.Client{ Transport: &http.Transport{ DialContext: (&net.Dialer{ Timeout: 30 * time.Second, KeepAlive: 30 * time.Second, }).DialContext, MaxIdleConns: 50000, MaxIdleConnsPerHost: 50000, IdleConnTimeout: 30 * time.Second, TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, TLSHandshakeTimeout: 3 * time.Second, ExpectContinueTimeout: 1 * time.Second, }, } }) return httpClient}
���������������HTTPS POST������
������������������������HTTP������������������������������������HTTPS POST���������������������������������������
func HttpsPost(url, stu string, body *[]byte) []byte { client := CreateHTTPClient() reader := bytes.NewReader(stu) request, err := http.NewRequest("POST", url, reader) if err != nil { return nil } request.Header.Set("Content-Type", "x-protobuf") request.Header.Set("Connection", "Keep-Alive") response, err := client.Do(request) if err != nil { return nil } defer response.Body.Close() *body, _ = ioutil.ReadAll(response.Body) return *body}
���������������
once
���������������������������HTTP���������������������������������������������������������������������������DialContext
���IdleConnTimeout
���������������������������������������������������InsecureSkipVerify: true
������������ ������ ���������������������/testing���������Content-Type
���Connection
���������������������������������������������������������������������������������Golang���������������������������������HTTP���������������������������������������������HTTPS POST���������
发表评论
最新留言
关于作者
