httpClient.go 946 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package service
  2. import (
  3. "crypto/md5"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "strconv"
  8. "go-common/library/log"
  9. )
  10. // HTTPClient http client handle
  11. func (s *Service) HTTPClient(method, url string, params map[string]string, nowTime int64) (body []byte, err error) {
  12. req, err := http.NewRequest(method, url, nil)
  13. if err != nil {
  14. log.Error("http.NewRequest error(%v)", err)
  15. return
  16. }
  17. q := req.URL.Query()
  18. for key, value := range params {
  19. q.Add(key, value)
  20. }
  21. q.Add("appkey", s.conf.KeySecret.Key)
  22. q.Add("ts", strconv.FormatInt(nowTime, 10))
  23. sign := q.Encode() + s.conf.KeySecret.Secret
  24. q.Add("sign", fmt.Sprintf("%x", md5.Sum([]byte(sign))))
  25. req.URL.RawQuery = q.Encode()
  26. resp, err := http.DefaultClient.Do(req)
  27. if err != nil {
  28. log.Error("http.DefaultClient.Do error(%v)", err)
  29. return
  30. }
  31. defer resp.Body.Close()
  32. body, err = ioutil.ReadAll(resp.Body)
  33. if err != nil {
  34. log.Error("ioutil.ReadAll error(%v)", err)
  35. }
  36. return
  37. }