From b6917db8e61e8def1835df35cf84af754f197a1d Mon Sep 17 00:00:00 2001 From: John Date: Mon, 23 Jul 2018 11:16:21 +0800 Subject: [PATCH] =?UTF-8?q?ghttp.Client=E5=A2=9E=E5=8A=A0HTTP=E8=B4=A6?= =?UTF-8?q?=E5=8F=B7=E5=AF=86=E7=A0=81=E9=AA=8C=E8=AF=81=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/net/ghttp/ghttp_client_request.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/g/net/ghttp/ghttp_client_request.go b/g/net/ghttp/ghttp_client_request.go index aa7bf73e7..b44c24a94 100644 --- a/g/net/ghttp/ghttp_client_request.go +++ b/g/net/ghttp/ghttp_client_request.go @@ -19,19 +19,21 @@ import ( // http客户端 type Client struct { - http.Client // 底层http client对象 - header map[string]string // header + http.Client // 底层http client对象 + header map[string]string // header + authUser string // HTTP基本权限设置:名称 + authPass string // HTTP基本权限设置:密码 } // http客户端对象指针 func NewClient() (*Client) { return &Client{ - http.Client { + Client : http.Client { Transport: &http.Transport { DisableKeepAlives: true, }, }, - make(map[string]string), + header : make(map[string]string), } } @@ -45,6 +47,12 @@ func (c *Client) SetTimeOut(t time.Duration) { c.Timeout = t } +// 设置HTTP访问账号密码 +func (c *Client) SetBasicAuth(user, pass string) { + c.authUser = user + c.authPass = pass +} + // GET请求 func (c *Client) Get(url string) (*ClientResponse, error) { return c.DoRequest("GET", url, []byte("")) @@ -102,6 +110,10 @@ func (c *Client) Post(url, data string) (*ClientResponse, error) { req.Header.Set(k, v) } } + // HTTP账号密码 + if len(c.authUser) > 0 { + req.SetBasicAuth(c.authUser, c.authPass) + } // 执行请求 resp, err := c.Do(req) if err != nil {