httpclient.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package enc_vehicle
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "encoding/json"
  6. "fmt"
  7. "gd_gateway/common.in/config"
  8. "gd_vehicle/common.in/utils"
  9. "github.com/tidwall/gjson"
  10. "io/ioutil"
  11. "net/http"
  12. "strings"
  13. "time"
  14. )
  15. const (
  16. /* APPKEY = "ad076e9268206f674e1ae08b1c75e18c"
  17. APPSEC = "2a7d6784b0c70a519b0465edf26b1326"
  18. HOST = "http://114.116.149.44:61001"
  19. */
  20. APPKEY = "69581375b8dfa08b88de7cbad1d7fca4"
  21. APPSEC = "96460526b508f975ab063cdcf730d372"
  22. HOST = "http://gdtest.cn"
  23. )
  24. func concatGetURL(url string, data map[string]string) string {
  25. if data == nil {
  26. return url
  27. }
  28. for k, v := range data {
  29. url = url + "&" + k + "=" + v
  30. }
  31. url = strings.Replace(url, "&", "?", 1)
  32. return url
  33. }
  34. func GdClientPost(api string, data map[string]interface{}) (result []byte, err error) {
  35. jsonData, err := json.Marshal(data)
  36. if err != nil {
  37. return nil, err
  38. }
  39. var realData = map[string]string{
  40. "data": string(jsonData),
  41. }
  42. jsonData, err = json.Marshal(realData)
  43. if err != nil {
  44. return nil, err
  45. }
  46. client := &http.Client{}
  47. client.Timeout = 10 * time.Second
  48. req, err := http.NewRequest("POST", api, bytes.NewBuffer(jsonData))
  49. if err != nil {
  50. return nil, err
  51. }
  52. timestamp := "1111"
  53. req.Header.Set("app_key", APPKEY)
  54. req.Header.Set("ts", timestamp)
  55. signtext := fmt.Sprintf("%s%s%s%s", timestamp, string(jsonData), APPKEY, APPSEC)
  56. req.Header.Set("sign", utils.MD5(signtext))
  57. req.Header.Set("Content-Type", "application/json")
  58. resp, err := client.Do(req)
  59. if err != nil {
  60. return nil, err
  61. }
  62. defer resp.Body.Close()
  63. result, err = ioutil.ReadAll(resp.Body)
  64. return
  65. }
  66. func GdClientGet(api string, data map[string]interface{}) (result []byte, err error) {
  67. jsonData, err := json.Marshal(data)
  68. if err != nil {
  69. return nil, err
  70. }
  71. client := &http.Client{}
  72. client.Timeout = 10 * time.Second
  73. rData := map[string]string{"data": string(jsonData)}
  74. req, err := http.NewRequest("GET", concatGetURL(api, rData), nil)
  75. if err != nil {
  76. return nil, err
  77. }
  78. timestamp := "1111"
  79. req.Header.Set("app_key", APPKEY)
  80. req.Header.Set("ts", timestamp)
  81. signtext := fmt.Sprintf("%s%s%s%s", timestamp, string(jsonData), APPKEY, APPSEC)
  82. req.Header.Set("sign", utils.MD5(signtext))
  83. req.Header.Set("Content-Type", "application/json")
  84. resp, err := client.Do(req)
  85. if err != nil {
  86. return nil, err
  87. }
  88. defer resp.Body.Close()
  89. result, err = ioutil.ReadAll(resp.Body)
  90. fmt.Println(string(result), " error: ", err)
  91. return
  92. }
  93. func GdClientCryptoGet(api string, data map[string]interface{}) (result []byte, err error) {
  94. jsonData, err := json.Marshal(data)
  95. if err != nil {
  96. return nil, err
  97. }
  98. client := &http.Client{}
  99. client.Timeout = 10 * time.Second
  100. bytes, _ := config.AesEncrypt(string(jsonData), APPSEC)
  101. rData := map[string]string{"data": hex.EncodeToString(bytes)}
  102. req, err := http.NewRequest("GET", concatGetURL(api, rData), nil)
  103. if err != nil {
  104. return nil, err
  105. }
  106. timestamp := "1111"
  107. req.Header.Set("app_key", APPKEY)
  108. req.Header.Set("ts", timestamp)
  109. signtext := fmt.Sprintf("%s%s%s%s", timestamp, hex.EncodeToString(bytes), APPKEY, APPSEC)
  110. req.Header.Set("sign", utils.MD5(signtext))
  111. req.Header.Set("Content-Type", "application/json")
  112. resp, err := client.Do(req)
  113. if err != nil {
  114. return nil, err
  115. }
  116. defer resp.Body.Close()
  117. rByte, err := ioutil.ReadAll(resp.Body)
  118. fmt.Println(string(rByte), " error: ", err)
  119. res := gjson.GetBytes(rByte, "data").String()
  120. // fmt.Println(res)
  121. result, err = config.AesDecrypt([]byte(res), []byte(APPSEC))
  122. // fmt.Println("err",err)
  123. //fmt.Println(string(result))
  124. return
  125. }