package main import ( "bytes" "encoding/json" "fmt" "io/ioutil" "net/http" "os" ) // 请求结构 type ClassificationRequest struct { Title string `json:"title"` Detail string `json:"detail"` } // 响应结构 type ClassificationResponse struct { PackageType int `json:"packageType"` Description string `json:"description"` IsJointVenture bool `json:"isJointVenture"` Success bool `json:"success"` ErrorMessage string `json:"errorMessage,omitempty"` Result string `json:"result"` } func main() { // 默认服务地址 serverURL := "http://localhost:8182/quote_classify" // 检查是否提供了自定义URL if len(os.Args) > 1 { serverURL = os.Args[1] } // 创建示例请求数据 requestData := ClassificationRequest{ Title: "XX市公共设施建设项目招标公告", Detail: `下浮:20%`, } // 发送请求 response, err := sendClassificationRequest(serverURL, requestData) if err != nil { fmt.Printf("请求失败: %v\n", err) return } // 处理响应 if !response.Success { fmt.Printf("分类失败: %s\n", response.ErrorMessage) return } // 输出结果 fmt.Printf("类型代码: %d\n", response.PackageType) fmt.Printf("类型描述: %s\n", response.Description) fmt.Printf("是否联合体投标: %v\n", response.IsJointVenture) } // 发送分类请求到服务器 func sendClassificationRequest(url string, requestData ClassificationRequest) (*ClassificationResponse, error) { // 转换请求数据为JSON jsonData, err := json.Marshal(requestData) if err != nil { return nil, fmt.Errorf("无法序列化请求数据: %v", err) } // 创建HTTP请求 req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) if err != nil { return nil, fmt.Errorf("无法创建HTTP请求: %v", err) } // 设置请求头 req.Header.Set("Content-Type", "application/json") // 发送请求 client := &http.Client{} resp, err := client.Do(req) if err != nil { return nil, fmt.Errorf("发送请求失败: %v", err) } defer resp.Body.Close() // 读取响应内容 body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("读取响应失败: %v", err) } // 检查HTTP状态码 if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("服务器返回错误: %s, 响应: %s", resp.Status, string(body)) } // 解析响应JSON var response ClassificationResponse if err := json.Unmarshal(body, &response); err != nil { return nil, fmt.Errorf("解析响应失败: %v, 响应内容: %s", err, string(body)) } return &response, nil }