1 /** 2 1.可设置代理 3 2.可设置 cookie 4 3.自动保存并应用响应的 cookie 5 4.自动为重新向的请求添加 cookie 6 */ 7 package curl 8 9 import ( 10 "net/http" 11 "net/url" 12 "io/ioutil" 13 "strings" 14 ) 15 16 type Browser struct { 17 cookies []*http.Cookie; 18 client *http.Client; 19 } 20 21 //初始化 22 func NewBrowser() *Browser { 23 hc := &Browser{}; 24 hc.client = &http.Client{}; 25 //为所有重定向的请求增加cookie 26 hc.client.CheckRedirect = func(req *http.Request, via []*http.Request) error { 27 if len(via) > 0 { 28 for _,v := range hc.GetCookie() { 29 req.AddCookie(v); 30 } 31 } 32 return nil 33 } 34 return hc; 35 } 36 37 //设置代理地址 38 func (self *Browser) SetProxyUrl(proxyUrl string) { 39 proxy := func(_ *http.Request) (*url.URL, error) { 40 return url.Parse(proxyUrl); 41 }; 42 transport := &http.Transport{Proxy:proxy}; 43 self.client.Transport = transport; 44 } 45 46 //设置请求cookie 47 func (self *Browser) AddCookie(cookies []*http.Cookie) { 48 self.cookies = append(self.cookies, cookies...); 49 } 50 51 //获取当前所有的cookie 52 func (self *Browser) GetCookie() ([]*http.Cookie) { 53 return self.cookies; 54 } 55 56 //发送Get请求 57 func (self *Browser) Get(requestUrl string) ([]byte, int) { 58 request,_ := http.NewRequest("GET", requestUrl, nil); 59 self.setRequestCookie(request); 60 response,_ := self.client.Do(request); 61 defer response.Body.Close(); 62 63 data, _ := ioutil.ReadAll(response.Body) 64 return data, response.StatusCode; 65 } 66 67 //发送Post请求 68 func (self *Browser) Post(requestUrl string, params map[string]string) ([]byte) { 69 postData := self.encodeParams(params); 70 request,_ := http.NewRequest("POST", requestUrl, strings.NewReader(postData)); 71 request.Header.Set("Content-Type", "application/x-www-form-urlencoded") 72 self.setRequestCookie(request); 73 74 response,_ := self.client.Do(request); 75 defer response.Body.Close(); 76 77 //保存响应的 cookie 78 respCks := response.Cookies(); 79 self.cookies = append(self.cookies, respCks...); 80 81 data, _ := ioutil.ReadAll(response.Body) 82 return data; 83 } 84 85 86 //为请求设置 cookie 87 func (self *Browser) setRequestCookie(request *http.Request) { 88 for _,v := range self.cookies{ 89 request.AddCookie(v) 90 } 91 } 92 93 //参数 encode 94 func (self *Browser) encodeParams(params map[string]string) string { 95 paramsData := url.Values{}; 96 for k,v := range params { 97 paramsData.Set(k,v); 98 } 99 return paramsData.Encode(); 100 }