From 30b9cedae5eb35ec7c0ca7a239517dbaa350ecb3 Mon Sep 17 00:00:00 2001 From: qydysky Date: Thu, 29 Oct 2020 02:05:45 +0800 Subject: [PATCH] 87 --- Reqf.go | 39 ++++++++++++++------------------------- 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/Reqf.go b/Reqf.go index 2760727..bac56b1 100644 --- a/Reqf.go +++ b/Reqf.go @@ -18,17 +18,13 @@ type Rval struct { Url string PostStr string Timeout int - Referer string - Cookie string Proxy string - ContentType string - Accept string - AcceptLanguage string - Connection string Retry int SleepTime int JustResponseCode bool SaveToPath string + + Header map[string]string } type req struct { @@ -84,23 +80,20 @@ func (this *req) Reqf_1(val Rval) (error) { var ( Url string = val.Url PostStr string = val.PostStr - Referer string = val.Referer - Cookie string = val.Cookie Proxy string = val.Proxy - Accept string = val.Accept - ContentType string = val.ContentType - Connection string = val.Connection - AcceptLanguage string = val.AcceptLanguage Timeout int = val.Timeout JustResponseCode bool =val.JustResponseCode SaveToPath string =val.SaveToPath + + Header map[string]string = val.Header ) var beginTime time.Time = time.Now() - var client http.Client + if Header == nil {Header = make(map[string]string)} + if Timeout != -1 { client.Timeout = time.Duration(Timeout)*time.Second } @@ -121,7 +114,7 @@ func (this *req) Reqf_1(val Rval) (error) { if len(PostStr) > 0 { Method = "POST"; body = strings.NewReader(PostStr); - if ContentType == "" {ContentType = "application/x-www-form-urlencoded"} + if _,ok := Header["ContentType"];!ok {Header["ContentType"] = "application/x-www-form-urlencoded"} } cx, cancel := context.WithCancel(context.Background()) @@ -134,19 +127,15 @@ func (this *req) Reqf_1(val Rval) (error) { <- this.cancel cancel() }() + + if _,ok := Header["Accept"];!ok {Header["Accept"] = `text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8`} + if _,ok := Header["Connection"];!ok {Header["Connection"] = "keep-alive"} + if _,ok := Header["User-Agent"];!ok {Header["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36"} - if Accept==""{Accept = `text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8`} - if Connection==""{Connection = "keep-alive"} - - if Cookie!=""{req.Header.Add("Cookie",Cookie)} - if Referer!=""{req.Header.Add("Referer",Referer)} - if Connection!=""{req.Header.Set("Connection",Connection)} - if AcceptLanguage!=""{req.Header.Set("Accept-Language",AcceptLanguage)} - if Accept!=""{req.Header.Add("Accept",Accept)} - if ContentType!=""{req.Header.Set("Content-Type", ContentType)} //添加请求头 + for k,v := range Header { + req.Header.Set(k, v) + } - req.Header.Add("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36") - resp, err := client.Do(req) if err != nil { -- 2.39.2