package main import ( "bytes" "context" "encoding/json" "errors" "fmt" "net/http" "time" ) // sendAi 调用大模型招标分类 func sendAi(data map[string]interface{}, url string) (res map[string]interface{}) { // 设置 2 秒的超时 ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) defer cancel() //url := "http://192.168.3.109:16688" jsonData, err := json.Marshal(data) if err != nil { fmt.Println("JSON marshal error:", err) return } req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) if err != nil { fmt.Println("Request error:", err) return } req.Header.Set("Content-Type", "application/json") // 将请求与上下文关联 req = req.WithContext(ctx) client := &http.Client{} resp, err := client.Do(req) if err != nil { // 使用 errors.Is 检查错误是否是超时错误 if errors.Is(err, context.DeadlineExceeded) { fmt.Println("Request timed out") return } fmt.Println("Request error:", err) return } defer resp.Body.Close() err = json.NewDecoder(resp.Body).Decode(&res) if err != nil { fmt.Println("Response decoding error:", err) return } //map[result:[结果-中标] status:200] return }