封装的一个http请求struct,包含:头、cookie、代理、超时、本地ip切换

 2023-09-05 阅读 19 评论 0

摘要:2019独角兽企业重金招聘Python工程师标准>>> package httpimport ("net""net/http""net/url""time""errors""strings" )var headerUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWeb

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

package httpimport ("net""net/http""net/url""time""errors""strings"
)var headerUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36"type Httpx struct {Url stringHeaders map[string] stringCookies []*http.CookieClientIP string //本机外网IP,可选Method stringProxyUrl string //代理URLPostData url.ValuesTimeout int //超时时间,秒
}func NewHttpx(reqUrl string) (h *Httpx) {headers := make(map[string] string)headers["User-Agent"] = headerUserAgentreturn &Httpx{Url : reqUrl,Headers : headers,Method : "GET",Timeout : 30,}
}//添加header
func (h *Httpx)AddHeader(key, value string) {h.Headers[key] = value
}//添加cookie
func (h *Httpx)AddCookie(c *http.Cookie) {h.Cookies = append(h.Cookies, c)
}//添加POST值
func (h *Httpx)AddPostValue(key string, values []string) {if h.PostData == nil {h.PostData = make(url.Values)}if values != nil {for _, v := range values {h.PostData.Add(key, v)}}
}//发送请求
func (h *Httpx)Send() (response *http.Response, err error) {if h.Url == "" {return nil, errors.New("URL is empty")}defer func(){if err != nil && h.ClientIP != "" {err = errors.New(err.Error() + " client ip is "+h.ClientIP)}}()var req *http.Requestif h.Method == "POST" {req, _ = http.NewRequest("POST", h.Url, strings.NewReader(h.PostData.Encode()))req.Header.Set("Content-Type", "application/x-www-form-urlencoded")}else{req, _ = http.NewRequest(h.Method, h.Url, nil)}//headersif len(h.Headers) >0 {for k, v := range h.Headers {req.Header.Set(k, v)}}//cookiesif len(h.Cookies) > 0 {for _, v := range h.Cookies {req.AddCookie(v)}}transport := &http.Transport{}//是否使用代理if h.ProxyUrl != "" {proxy, err := url.Parse(h.ProxyUrl)if err != nil {return nil, err}transport.Proxy = http.ProxyURL(proxy)}//设置超时时间dialer := net.Dialer{Timeout : time.Duration(h.Timeout) * time.Second,Deadline : time.Now().Add(time.Duration(h.Timeout) * time.Second),}//是否使用指定的IP发送请求if h.ClientIP != "" {transport.Dial = func(network, address string) (net.Conn, error) {//本地地址  本地外网IPlAddr, err := net.ResolveTCPAddr(network, h.ClientIP+":0")if err != nil {return nil, err}dialer.LocalAddr = lAddrreturn dialer.Dial(network, address)}}else {transport.Dial = func(network, address string) (net.Conn, error) {return dialer.Dial(network, address)}}client := &http.Client{Transport: transport,}response, err = client.Do(req)return response, err}
// GET请求
func HttpGet(reqUrl string) (*http.Response, error) {hx := NewHttpx(reqUrl)return hx.Send()
}//利用指定的IP发送请求
func HttpGetFromIP(reqUrl, ipaddr string) (*http.Response, error) {hx := NewHttpx(reqUrl)hx.ClientIP = ipaddrreturn hx.Send()
}// http GET 代理
func HttpGetFromProxy(reqUrl, proxyURL string) (*http.Response, error) {hx := NewHttpx(reqUrl)hx.ProxyUrl = proxyURLreturn hx.Send()
}//POST请求
func HttpPost(reqUrl string, postValues map[string] []string) (*http.Response, error) {hx := NewHttpx(reqUrl)hx.Method = "POST"if postValues != nil {for k, v := range postValues {hx.AddPostValue(k, v)}}return hx.Send()
}

转载于:https://my.oschina.net/mejinke/blog/145900

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/1999.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息