From 93ebaeea5a8c8f0fefa109b8ac186622f2ec384e Mon Sep 17 00:00:00 2001 From: liyp Date: Sat, 17 Aug 2024 18:04:24 +0800 Subject: [PATCH] =?UTF-8?q?feat(workers):=20=E6=9B=B4=E6=96=B0httpcat?= =?UTF-8?q?=E5=B7=A5=E4=BD=9C=E5=99=A8=E4=BB=A5=E5=A4=84=E7=90=86=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E9=94=99=E8=AF=AF=E5=92=8C=E6=9B=B4=E6=96=B0User-Agen?= =?UTF-8?q?t?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/testrequest.go | 38 ++++++++++++++++++++++++++++++++++++++ workers/httpcat.go | 30 +++++++++++++++--------------- 2 files changed, 53 insertions(+), 15 deletions(-) create mode 100644 test/testrequest.go diff --git a/test/testrequest.go b/test/testrequest.go new file mode 100644 index 0000000..9983dae --- /dev/null +++ b/test/testrequest.go @@ -0,0 +1,38 @@ +package main + +import ( + "fmt" + "io/ioutil" + "net/http" +) + +func main() { + + url := "https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Status/429" + method := "GET" + + client := &http.Client{} + req, err := http.NewRequest(method, url, nil) + + if err != nil { + fmt.Println(err) + return + } + req.Header.Add("User-Agent", "Apifox/1.0.0 (https://apifox.com)") + req.Header.Add("Accept", "*/*") + req.Header.Add("Host", "developer.mozilla.org") + + res, err := client.Do(req) + if err != nil { + fmt.Println(err) + return + } + defer res.Body.Close() + + body, err := ioutil.ReadAll(res.Body) + if err != nil { + fmt.Println(err) + return + } + fmt.Println(string(body)) +} diff --git a/workers/httpcat.go b/workers/httpcat.go index 6268898..cb818bc 100644 --- a/workers/httpcat.go +++ b/workers/httpcat.go @@ -4,6 +4,7 @@ import ( "compress/gzip" "fmt" "io" + "log" "github.com/PuerkitoBio/goquery" "github.com/andybalholm/brotli" @@ -42,35 +43,34 @@ func (a *HttpCat) GetMsg() string { } respContent := "[CQ:image,file=" + picUrl + "]" docUrl := "https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Status/" + a.Parms[1] - resp, _, _ = request.Get(docUrl). + res, _, errs := request.Get(docUrl). Set("Host", "developer.mozilla.org"). - Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:131.0) Gecko/20100101 Firefox/131.0"). + Set("User-Agent", "Apifox/1.0.0 (https://apifox.com)"). Set("Accept", "*/*"). - Set("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2"). - Set("Accept-Encoding", "gzip, deflate, br, zstd"). - Set("Connection", "keep-alive"). - Set("Referer", docUrl). - Set("Sec-Fetch-Dest", "empty"). - Set("Sec-Fetch-Mode", "no-cors"). - Set("Sec-Fetch-Site", "same-origin"). + Set("Content-Type", "text/plain"). End() - - if resp.StatusCode == 200 { + if len(errs) > 0 { + log.Println("请求错误: " + fmt.Sprint(errs)) + } + if res != nil { + defer res.Body.Close() + } + if res != nil && res.StatusCode == 200 { // 解压缩响应体 var reader io.ReadCloser - switch resp.Header.Get("Content-Encoding") { + switch res.Header.Get("Content-Encoding") { case "br": - reader = io.NopCloser(brotli.NewReader(resp.Body)) + reader = io.NopCloser(brotli.NewReader(res.Body)) case "gzip": var err error - reader, err = gzip.NewReader(resp.Body) + reader, err = gzip.NewReader(res.Body) if err != nil { fmt.Println("gzip 解压错误:", err) return respContent } defer reader.Close() default: - reader = resp.Body + reader = res.Body } // 使用 goquery 解析 HTML