http请求添加header和代理

This commit is contained in:
learnmore2019
2019-02-15 10:49:38 +08:00
parent 6945fc8532
commit bc82d200b0
2 changed files with 19 additions and 6 deletions

View File

@@ -6,6 +6,7 @@ import (
"io/ioutil" "io/ioutil"
"net" "net"
"net/http" "net/http"
"net/url"
"time" "time"
"github.com/duanhf2012/origin/service" "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{ slf.client = &http.Client{
Transport: &http.Transport{ Transport: &http.Transport{
DialContext: (&net.Dialer{ DialContext: (&net.Dialer{
@@ -51,22 +60,23 @@ func (slf *HttpClientPoolModule) Init(maxpool int) {
MaxIdleConns: maxpool, MaxIdleConns: maxpool,
MaxIdleConnsPerHost: maxpool, MaxIdleConnsPerHost: maxpool,
IdleConnTimeout: 60 * time.Second, 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{ ret := SyncHttpRespone{
resp: make(chan HttpRespone, 1), resp: make(chan HttpRespone, 1),
} }
go func() { go func() {
rsp := slf.Request(method, url, body) rsp := slf.Request(method, url, body, header)
ret.resp <- rsp ret.resp <- rsp
}() }()
return ret 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 { if slf.client == nil {
panic("Call the init function first") panic("Call the init function first")
} }
@@ -76,6 +86,9 @@ func (slf *HttpClientPoolModule) Request(method string, url string, body []byte)
ret.Err = err ret.Err = err
return ret return ret
} }
if header != nil {
req.Header = header
}
rsp, err := slf.client.Do(req) rsp, err := slf.client.Do(req)
if err != nil { if err != nil {
ret.Err = err ret.Err = err

View File

@@ -12,14 +12,14 @@ func TestHttpClientPoolModule(t *testing.T) {
c := sysmodule.HttpClientPoolModule{} c := sysmodule.HttpClientPoolModule{}
c.Init(10) 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.Err)
fmt.Println(rsp.Header) fmt.Println(rsp.Header)
fmt.Println(rsp.StatusCode) fmt.Println(rsp.StatusCode)
fmt.Println(rsp.Status) fmt.Println(rsp.Status)
fmt.Println(string(rsp.Body)) 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) rsp1 := srsp.Get(1)
fmt.Println(rsp1.Err) fmt.Println(rsp1.Err)
fmt.Println(rsp1.Header) fmt.Println(rsp1.Header)