client.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package http
  2. import (
  3. "net/http"
  4. "strings"
  5. "time"
  6. "kpt.notice/pkg/log"
  7. )
  8. type Client struct {
  9. client *http.Client
  10. }
  11. func NewClient(timeout time.Duration) *Client {
  12. return &Client{
  13. client: &http.Client{
  14. Timeout: timeout,
  15. },
  16. }
  17. }
  18. func HttpPost(url, contentType, body string) *http.Response {
  19. // body := `{"userinfo":"tmr.shengmu23.bbc","openid":"123"}`
  20. // url :="http://kpttest.kptyun.com/userwxopenid/binding"
  21. // contentType:="application/json"
  22. log.Errorf("HttpPost====enter body=======%+v\n", body)
  23. resp, err := http.Post(url, contentType, strings.NewReader(body))
  24. if err != nil || resp.StatusCode != 200 {
  25. log.Errorf("post err ====%+v===%+v\n", err, resp.StatusCode)
  26. return nil
  27. }
  28. defer resp.Body.Close()
  29. log.Errorf("resp=====%+v\n", resp.StatusCode)
  30. return resp
  31. }
  32. func HttpGet(url string, params map[string]string) *http.Response {
  33. // url :="http://kpttest.kptyun.com/userwxopenid/binding"
  34. // contentType:="application/json"
  35. // params := map[string]string{"userinfo":"tmr.shengmu23.bbc","openid":"123"}
  36. s := make([]string, len(params))
  37. for k, v := range params {
  38. s = append(s, k+"="+v)
  39. }
  40. url = url + "?" + strings.Join(s, "&")
  41. resp, err := http.Get(url)
  42. if err != nil || resp.StatusCode != 200 {
  43. return nil
  44. }
  45. defer resp.Body.Close()
  46. return resp
  47. }