添加uaHook

This commit is contained in:
eatMoreApple 2021-05-11 17:00:46 +08:00
parent 00f7ca4f93
commit 5d8dea264c

View File

@ -59,7 +59,9 @@ func DefaultClient(urlManager UrlManager) *Client {
},
Jar: jar,
}
return NewClient(client, urlManager)
c := NewClient(client, urlManager)
c.AddHttpHook(UserAgentHook{})
return c
}
func (c *Client) AddHttpHook(hooks ...HttpHook) {
@ -77,13 +79,7 @@ func (c *Client) do(req *http.Request) (*http.Response, error) {
return resp, err
}
// 抽象Do方法,将所有的有效的cookie存入Client.cookies
// 方便热登陆时获取
func (c *Client) Do(req *http.Request) (*http.Response, error) {
resp, err := c.do(req)
if err != nil {
return resp, err
}
func (c *Client) setCookie(resp *http.Response) {
c.mu.Lock()
defer c.mu.Unlock()
cookies := resp.Cookies()
@ -91,6 +87,15 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) {
c.cookies = make(map[string][]*http.Cookie)
}
c.cookies[resp.Request.URL.String()] = cookies
}
// 抽象Do方法,将所有的有效的cookie存入Client.cookies
// 方便热登陆时获取
func (c *Client) Do(req *http.Request) (*http.Response, error) {
resp, err := c.do(req)
if err == nil {
c.setCookie(resp)
}
return resp, err
}