简化 HTTP 请求创建逻辑;修复报告请求失败问题

This commit is contained in:
2025-05-17 11:12:45 +08:00
parent c1664aa898
commit 14a778101d

View File

@@ -23,7 +23,7 @@ func Online(prov, city, isp string) (id int32, host string, err error) {
ispInt = 3 ispInt = 3
} }
body, err := json.Marshal(map[string]any{ bytes, err := json.Marshal(map[string]any{
"prov": prov, "prov": prov,
"city": city, "city": city,
"isp": ispInt, "isp": ispInt,
@@ -33,14 +33,9 @@ func Online(prov, city, isp string) (id int32, host string, err error) {
if err != nil { if err != nil {
return 0, "", err return 0, "", err
} }
var body = strings.NewReader(string(bytes))
req, err := http.NewRequest("POST", env.EndpointOnline, strings.NewReader(string(body))) resp, err := http.Post(env.EndpointOnline, "application/json", body)
if err != nil {
return 0, "", fmt.Errorf("创建请求失败: %w", err)
}
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {
return 0, "", fmt.Errorf("执行请求失败: %w", err) return 0, "", fmt.Errorf("执行请求失败: %w", err)
} }
@@ -49,7 +44,7 @@ func Online(prov, city, isp string) (id int32, host string, err error) {
return 0, "", errors.New("状态码: " + resp.Status) return 0, "", errors.New("状态码: " + resp.Status)
} }
bytes, err := io.ReadAll(resp.Body) bytes, err = io.ReadAll(resp.Body)
if err != nil { if err != nil {
return 0, "", fmt.Errorf("读取响应失败: %w", err) return 0, "", fmt.Errorf("读取响应失败: %w", err)
} }
@@ -77,12 +72,7 @@ func Offline() error {
} }
var body = strings.NewReader(string(bytes)) var body = strings.NewReader(string(bytes))
req, err := http.NewRequest("POST", env.EndpointOffline, body) resp, err := http.Post(env.EndpointOffline, "application/json", body)
if err != nil {
return fmt.Errorf("创建请求失败: %w", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {
return fmt.Errorf("执行请求失败: %w", err) return fmt.Errorf("执行请求失败: %w", err)
} }