main.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "os"
  9. )
  10. // 请求结构
  11. type ClassificationRequest struct {
  12. Title string `json:"title"`
  13. Detail string `json:"detail"`
  14. }
  15. // 响应结构
  16. type ClassificationResponse struct {
  17. PackageType int `json:"packageType"`
  18. Description string `json:"description"`
  19. IsJointVenture bool `json:"isJointVenture"`
  20. Success bool `json:"success"`
  21. ErrorMessage string `json:"errorMessage,omitempty"`
  22. Result string `json:"result"`
  23. }
  24. func main() {
  25. // 默认服务地址
  26. serverURL := "http://localhost:8182/quote_classify"
  27. // 检查是否提供了自定义URL
  28. if len(os.Args) > 1 {
  29. serverURL = os.Args[1]
  30. }
  31. // 创建示例请求数据
  32. requestData := ClassificationRequest{
  33. Title: "XX市公共设施建设项目招标公告",
  34. Detail: `下浮:20%`,
  35. }
  36. // 发送请求
  37. response, err := sendClassificationRequest(serverURL, requestData)
  38. if err != nil {
  39. fmt.Printf("请求失败: %v\n", err)
  40. return
  41. }
  42. // 处理响应
  43. if !response.Success {
  44. fmt.Printf("分类失败: %s\n", response.ErrorMessage)
  45. return
  46. }
  47. // 输出结果
  48. fmt.Printf("类型代码: %d\n", response.PackageType)
  49. fmt.Printf("类型描述: %s\n", response.Description)
  50. fmt.Printf("是否联合体投标: %v\n", response.IsJointVenture)
  51. }
  52. // 发送分类请求到服务器
  53. func sendClassificationRequest(url string, requestData ClassificationRequest) (*ClassificationResponse, error) {
  54. // 转换请求数据为JSON
  55. jsonData, err := json.Marshal(requestData)
  56. if err != nil {
  57. return nil, fmt.Errorf("无法序列化请求数据: %v", err)
  58. }
  59. // 创建HTTP请求
  60. req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
  61. if err != nil {
  62. return nil, fmt.Errorf("无法创建HTTP请求: %v", err)
  63. }
  64. // 设置请求头
  65. req.Header.Set("Content-Type", "application/json")
  66. // 发送请求
  67. client := &http.Client{}
  68. resp, err := client.Do(req)
  69. if err != nil {
  70. return nil, fmt.Errorf("发送请求失败: %v", err)
  71. }
  72. defer resp.Body.Close()
  73. // 读取响应内容
  74. body, err := ioutil.ReadAll(resp.Body)
  75. if err != nil {
  76. return nil, fmt.Errorf("读取响应失败: %v", err)
  77. }
  78. // 检查HTTP状态码
  79. if resp.StatusCode != http.StatusOK {
  80. return nil, fmt.Errorf("服务器返回错误: %s, 响应: %s", resp.Status, string(body))
  81. }
  82. // 解析响应JSON
  83. var response ClassificationResponse
  84. if err := json.Unmarshal(body, &response); err != nil {
  85. return nil, fmt.Errorf("解析响应失败: %v, 响应内容: %s", err, string(body))
  86. }
  87. return &response, nil
  88. }