From bc82d200b08ee714b6c336192921da7435f1a9fd Mon Sep 17 00:00:00 2001 From: learnmore2019 <772381604@qq.com> Date: Fri, 15 Feb 2019 10:49:38 +0800 Subject: [PATCH] =?UTF-8?q?http=E8=AF=B7=E6=B1=82=E6=B7=BB=E5=8A=A0header?= =?UTF-8?q?=E5=92=8C=E4=BB=A3=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sysmodule/HttpClientPoolModule.go | 21 +++++++++++++++++---- sysmodule/HttpClientPoolModule_test.go | 4 ++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/sysmodule/HttpClientPoolModule.go b/sysmodule/HttpClientPoolModule.go index cda6bfe..ddd2e37 100644 --- a/sysmodule/HttpClientPoolModule.go +++ b/sysmodule/HttpClientPoolModule.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "net" "net/http" + "net/url" "time" "github.com/duanhf2012/origin/service" @@ -41,7 +42,15 @@ func (slf *SyncHttpRespone) Get(timeoutMs int) HttpRespone { } } -func (slf *HttpClientPoolModule) Init(maxpool int) { +func (slf *HttpClientPoolModule) Init(maxpool int, proxyUrl string) { + type ProxyFun func(_ *http.Request) (*url.URL, error) + var proxyfun ProxyFun + if proxyUrl != "" { + proxyfun = func(_ *http.Request) (*url.URL, error) { + return url.Parse(proxyUrl) + } + } + slf.client = &http.Client{ Transport: &http.Transport{ DialContext: (&net.Dialer{ @@ -51,22 +60,23 @@ func (slf *HttpClientPoolModule) Init(maxpool int) { MaxIdleConns: maxpool, MaxIdleConnsPerHost: maxpool, IdleConnTimeout: 60 * time.Second, + Proxy: proxyfun, }, } } -func (slf *HttpClientPoolModule) SyncRequest(method string, url string, body []byte) SyncHttpRespone { +func (slf *HttpClientPoolModule) SyncRequest(method string, url string, body []byte, header http.Header) SyncHttpRespone { ret := SyncHttpRespone{ resp: make(chan HttpRespone, 1), } go func() { - rsp := slf.Request(method, url, body) + rsp := slf.Request(method, url, body, header) ret.resp <- rsp }() return ret } -func (slf *HttpClientPoolModule) Request(method string, url string, body []byte) HttpRespone { +func (slf *HttpClientPoolModule) Request(method string, url string, body []byte, header http.Header) HttpRespone { if slf.client == nil { panic("Call the init function first") } @@ -76,6 +86,9 @@ func (slf *HttpClientPoolModule) Request(method string, url string, body []byte) ret.Err = err return ret } + if header != nil { + req.Header = header + } rsp, err := slf.client.Do(req) if err != nil { ret.Err = err diff --git a/sysmodule/HttpClientPoolModule_test.go b/sysmodule/HttpClientPoolModule_test.go index 61360f0..7089a04 100644 --- a/sysmodule/HttpClientPoolModule_test.go +++ b/sysmodule/HttpClientPoolModule_test.go @@ -12,14 +12,14 @@ func TestHttpClientPoolModule(t *testing.T) { c := sysmodule.HttpClientPoolModule{} c.Init(10) - rsp := c.Request(http.MethodGet, "https://www.baidu.com/", nil) + rsp := c.Request(http.MethodGet, "https://www.baidu.com/", nil, nil) fmt.Println(rsp.Err) fmt.Println(rsp.Header) fmt.Println(rsp.StatusCode) fmt.Println(rsp.Status) fmt.Println(string(rsp.Body)) - srsp := c.SyncRequest(http.MethodGet, "https://www.baidu.com/", nil) + srsp := c.SyncRequest(http.MethodGet, "https://www.baidu.com/", nil, nil) rsp1 := srsp.Get(1) fmt.Println(rsp1.Err) fmt.Println(rsp1.Header)