mirror of
https://github.com/oneclickvirt/basics.git
synced 2025-10-08 01:50:28 +08:00
v0.0.5 - 更新请求实现方式
This commit is contained in:
@@ -9,6 +9,8 @@ import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/imroc/req/v3"
|
||||
)
|
||||
|
||||
// FetchJsonFromURL 函数用于从指定的 URL 获取信息
|
||||
@@ -22,49 +24,47 @@ func FetchJsonFromURL(url, netType string, enableHeader bool, additionalHeader s
|
||||
if netType != "tcp4" && netType != "tcp6" {
|
||||
return nil, fmt.Errorf("Invalid netType: %s. Expected 'tcp4' or 'tcp6'.", netType)
|
||||
}
|
||||
|
||||
// 创建 HTTP 客户端
|
||||
client := &http.Client{
|
||||
Timeout: 6 * time.Second,
|
||||
Transport: &http.Transport{
|
||||
DialContext: func(ctx context.Context, network string, addr string) (net.Conn, error) {
|
||||
return (&net.Dialer{}).DialContext(ctx, netType, addr)
|
||||
},
|
||||
TLSHandshakeTimeout: 3 * time.Second,
|
||||
ResponseHeaderTimeout: 3 * time.Second,
|
||||
ExpectContinueTimeout: 3 * time.Second,
|
||||
},
|
||||
}
|
||||
// 创建 HTTP 请求
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error creating request: %v", err)
|
||||
}
|
||||
client := req.C()
|
||||
client.SetTimeout(6 * time.Second).
|
||||
SetDial(func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
return (&net.Dialer{}).DialContext(ctx, netType, addr)
|
||||
}).
|
||||
SetTLSHandshakeTimeout(3 * time.Second).
|
||||
SetResponseHeaderTimeout(3 * time.Second).
|
||||
SetExpectContinueTimeout(3 * time.Second)
|
||||
client.R().
|
||||
SetRetryCount(2).
|
||||
SetRetryBackoffInterval(1*time.Second, 2*time.Second).
|
||||
SetRetryFixedInterval(1 * time.Second)
|
||||
// 如果启用请求头,则设置请求头信息
|
||||
if enableHeader {
|
||||
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36")
|
||||
req.Header.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")
|
||||
client.Headers = make(http.Header)
|
||||
client.ImpersonateChrome()
|
||||
if additionalHeader != "" {
|
||||
tempList := strings.Split(additionalHeader, ":")
|
||||
if len(tempList) == 2 {
|
||||
req.Header.Set(tempList[0], tempList[1])
|
||||
client.Headers.Set(tempList[0], tempList[1])
|
||||
} else if len(tempList) > 2 {
|
||||
req.Header.Set(tempList[0], strings.Join(tempList[1:], ":"))
|
||||
client.Headers.Set(tempList[0], strings.Join(tempList[1:], ":"))
|
||||
}
|
||||
}
|
||||
}
|
||||
// 执行 HTTP 请求
|
||||
resp, err := client.Do(req)
|
||||
// 执行请求
|
||||
resp, err := client.R().Get(url)
|
||||
if err != nil {
|
||||
//fmt.Printf("Error fetching %s info: %v \n", url, err)
|
||||
return nil, fmt.Errorf("Error fetching %s info: %v", url, err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
// 检查响应状态码
|
||||
if !resp.IsSuccess() {
|
||||
return nil, fmt.Errorf("Error fetching %s info: status code %d", url, resp.StatusCode)
|
||||
}
|
||||
// 解析 JSON 响应体
|
||||
var data map[string]interface{}
|
||||
err = json.NewDecoder(resp.Body).Decode(&data)
|
||||
err = json.Unmarshal(resp.Bytes(), &data)
|
||||
if err != nil {
|
||||
//fmt.Printf("Error decoding %s info: %v \n", url, err)
|
||||
return nil, fmt.Errorf("Error decoding %s info: %v ", url, err)
|
||||
return nil, fmt.Errorf("Error decoding %s info: %v", url, err)
|
||||
}
|
||||
// 返回解析后的数据和 nil 错误
|
||||
return data, nil
|
||||
|
Reference in New Issue
Block a user