http.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package utils
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "time"
  10. )
  11. // HTTPClient HTTP客户端工具
  12. type HTTPClient struct {
  13. client *http.Client
  14. }
  15. // NewHTTPClient 创建HTTP客户端实例
  16. func NewHTTPClient(timeout int) *HTTPClient {
  17. return &HTTPClient{
  18. client: &http.Client{
  19. Timeout: time.Duration(timeout) * time.Second,
  20. },
  21. }
  22. }
  23. // Get 发送GET请求
  24. func (h *HTTPClient) Get(url string, headers map[string]string) ([]byte, error) {
  25. req, err := http.NewRequest("GET", url, nil)
  26. if err != nil {
  27. return nil, err
  28. }
  29. // 添加请求头
  30. for key, value := range headers {
  31. req.Header.Set(key, value)
  32. }
  33. return h.doRequest(req)
  34. }
  35. // Post 发送POST请求
  36. func (h *HTTPClient) Post(url string, data interface{}, headers map[string]string) ([]byte, error) {
  37. // 序列化数据
  38. jsonData, err := json.Marshal(data)
  39. if err != nil {
  40. return nil, err
  41. }
  42. // 创建请求
  43. req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
  44. if err != nil {
  45. return nil, err
  46. }
  47. // 设置Content-Type
  48. req.Header.Set("Content-Type", "application/json")
  49. // 添加其他请求头
  50. for key, value := range headers {
  51. req.Header.Set(key, value)
  52. }
  53. return h.doRequest(req)
  54. }
  55. // Put 发送PUT请求
  56. func (h *HTTPClient) Put(url string, data interface{}, headers map[string]string) ([]byte, error) {
  57. // 序列化数据
  58. jsonData, err := json.Marshal(data)
  59. if err != nil {
  60. return nil, err
  61. }
  62. // 创建请求
  63. req, err := http.NewRequest("PUT", url, bytes.NewBuffer(jsonData))
  64. if err != nil {
  65. return nil, err
  66. }
  67. // 设置Content-Type
  68. req.Header.Set("Content-Type", "application/json")
  69. // 添加其他请求头
  70. for key, value := range headers {
  71. req.Header.Set(key, value)
  72. }
  73. return h.doRequest(req)
  74. }
  75. // Delete 发送DELETE请求
  76. func (h *HTTPClient) Delete(url string, headers map[string]string) ([]byte, error) {
  77. req, err := http.NewRequest("DELETE", url, nil)
  78. if err != nil {
  79. return nil, err
  80. }
  81. // 添加请求头
  82. for key, value := range headers {
  83. req.Header.Set(key, value)
  84. }
  85. return h.doRequest(req)
  86. }
  87. // doRequest 执行HTTP请求
  88. func (h *HTTPClient) doRequest(req *http.Request) ([]byte, error) {
  89. // 创建带超时的上下文
  90. ctx, cancel := context.WithTimeout(context.Background(), h.client.Timeout)
  91. defer cancel()
  92. req = req.WithContext(ctx)
  93. // 发送请求
  94. resp, err := h.client.Do(req)
  95. if err != nil {
  96. return nil, err
  97. }
  98. defer resp.Body.Close()
  99. // 读取响应
  100. body, err := ioutil.ReadAll(resp.Body)
  101. if err != nil {
  102. return nil, err
  103. }
  104. // 检查响应状态码
  105. if resp.StatusCode < 200 || resp.StatusCode >= 300 {
  106. return nil, fmt.Errorf("HTTP请求失败,状态码: %d,响应内容: %s", resp.StatusCode, string(body))
  107. }
  108. return body, nil
  109. }