feat(workers): 更新httpcat工作器以处理请求错误和更新User-Agent

This commit is contained in:
liyp 2024-08-17 18:04:24 +08:00
parent b64fff11b6
commit 93ebaeea5a
2 changed files with 53 additions and 15 deletions

38
test/testrequest.go Normal file
View file

@ -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))
}

View file

@ -4,6 +4,7 @@ import (
"compress/gzip" "compress/gzip"
"fmt" "fmt"
"io" "io"
"log"
"github.com/PuerkitoBio/goquery" "github.com/PuerkitoBio/goquery"
"github.com/andybalholm/brotli" "github.com/andybalholm/brotli"
@ -42,35 +43,34 @@ func (a *HttpCat) GetMsg() string {
} }
respContent := "[CQ:image,file=" + picUrl + "]" respContent := "[CQ:image,file=" + picUrl + "]"
docUrl := "https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Status/" + a.Parms[1] 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("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", "*/*").
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("Content-Type", "text/plain").
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").
End() End()
if len(errs) > 0 {
if resp.StatusCode == 200 { log.Println("请求错误: " + fmt.Sprint(errs))
}
if res != nil {
defer res.Body.Close()
}
if res != nil && res.StatusCode == 200 {
// 解压缩响应体 // 解压缩响应体
var reader io.ReadCloser var reader io.ReadCloser
switch resp.Header.Get("Content-Encoding") { switch res.Header.Get("Content-Encoding") {
case "br": case "br":
reader = io.NopCloser(brotli.NewReader(resp.Body)) reader = io.NopCloser(brotli.NewReader(res.Body))
case "gzip": case "gzip":
var err error var err error
reader, err = gzip.NewReader(resp.Body) reader, err = gzip.NewReader(res.Body)
if err != nil { if err != nil {
fmt.Println("gzip 解压错误:", err) fmt.Println("gzip 解压错误:", err)
return respContent return respContent
} }
defer reader.Close() defer reader.Close()
default: default:
reader = resp.Body reader = res.Body
} }
// 使用 goquery 解析 HTML // 使用 goquery 解析 HTML