http_request.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package utils
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "strings"
  8. )
  9. // PostEdbLibRequest 调用指标接口
  10. func PostEdbLibRequest(param map[string]interface{}, method string) (result []byte, err error) {
  11. postUrl := EDB_LIB_URL + method
  12. postData, err := json.Marshal(param)
  13. if err != nil {
  14. return
  15. }
  16. result, err = HttpPostRequest(postUrl, string(postData), "application/json")
  17. if err != nil {
  18. return
  19. }
  20. return
  21. }
  22. func HttpPostRequest(url, postData string, params ...string) ([]byte, error) {
  23. fmt.Println("HttpPost Url:" + url)
  24. body := ioutil.NopCloser(strings.NewReader(postData))
  25. client := &http.Client{}
  26. req, err := http.NewRequest("POST", url, body)
  27. if err != nil {
  28. return nil, err
  29. }
  30. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  31. if len(params) > 0 && params[0] != "" {
  32. contentType = params[0]
  33. }
  34. req.Header.Set("Content-Type", contentType)
  35. req.Header.Set("authorization", MD5(APP_EDB_LIB_NAME_EN+EDB_LIB_Md5_KEY))
  36. resp, err := client.Do(req)
  37. if err != nil {
  38. fmt.Println("client.Do err:" + err.Error())
  39. return nil, err
  40. }
  41. defer resp.Body.Close()
  42. b, err := ioutil.ReadAll(resp.Body)
  43. if err != nil {
  44. fmt.Println("HttpPost:" + string(b))
  45. }
  46. return b, err
  47. }
  48. func HttpGetRequest(url string) ([]byte, error) {
  49. res, err := http.Get(url)
  50. if err != nil {
  51. return nil, err
  52. }
  53. defer res.Body.Close()
  54. return ioutil.ReadAll(res.Body)
  55. }