main.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package main
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "encoding/json"
  6. "fmt"
  7. "io/ioutil"
  8. "log"
  9. "net"
  10. "net/http"
  11. "net/url"
  12. "qfw/util"
  13. "strings"
  14. "time"
  15. )
  16. var (
  17. config map[string]string
  18. appid, key, apiurl, day string
  19. httpClient *http.Client
  20. MaxIdleCons int = 100
  21. MaxIdleConsPerHost int = 100
  22. IdleConnTimeout int = 2048
  23. ConnectTimeOut int = 30
  24. KeepAlive int = 30
  25. )
  26. func init() {
  27. httpClient = createHttpClient()
  28. }
  29. func main() {
  30. util.ReadConfig(&config)
  31. appid = config["appid"]
  32. key = config["key"]
  33. apiurl = config["apiurl"]
  34. day = config["day"]
  35. getData()
  36. }
  37. func createHttpClient() *http.Client {
  38. client := &http.Client{
  39. Transport: &http.Transport{
  40. Proxy: http.ProxyFromEnvironment,
  41. DialContext: (&net.Dialer{
  42. Timeout: time.Duration(ConnectTimeOut) * time.Second, //TCP连接超时30s
  43. KeepAlive: time.Duration(KeepAlive) * time.Second, //TCP keepalive保活检测定时30s
  44. }).DialContext,
  45. MaxIdleConns: MaxIdleCons,
  46. MaxIdleConnsPerHost: MaxIdleConsPerHost,
  47. IdleConnTimeout: time.Duration(IdleConnTimeout) * time.Second, //闲置连接超时2048s
  48. ResponseHeaderTimeout: time.Second * 60,
  49. },
  50. }
  51. return client
  52. }
  53. func getToken() (token string) {
  54. tm := fmt.Sprintf("%d", time.Now().Unix())
  55. res := post(apiurl+"/user/access_token", map[string]string{
  56. "appid": appid,
  57. "timestamp": tm,
  58. "signature": MD5(appid + tm + key),
  59. //"key": "6PzV0CUa",
  60. }, nil)
  61. log.Println(tm, MD5(appid+tm+key), res)
  62. if res != nil && res["access_token"] != "" {
  63. token, _ = res["access_token"].(string)
  64. }
  65. return
  66. }
  67. func getData() {
  68. // token := getToken()
  69. tm := fmt.Sprint(time.Now().Unix())
  70. data := post(apiurl+"/data/getalldata", map[string]string{
  71. // "access_token": token,
  72. "day": day,
  73. "next": "0",
  74. "appid": appid,
  75. }, map[string]string{
  76. "timestamp": tm,
  77. "signature": MD5(appid + tm + key),
  78. })
  79. //s, _ := json.Marshal(data["data"])
  80. //delete(data, "data")
  81. log.Println("tm", appid, tm)
  82. log.Println(MD5(appid+tm+key), data)
  83. }
  84. func MD5(str string) string {
  85. h := md5.New()
  86. h.Write([]byte(str))
  87. return strings.ToUpper(hex.EncodeToString(h.Sum(nil)))
  88. }
  89. func post(urls string, form, header map[string]string) (data map[string]interface{}) {
  90. formValues := make(url.Values)
  91. for k, v := range form {
  92. formValues[k] = []string{v}
  93. }
  94. log.Println(formValues)
  95. header["Content-Type"] = "application/x-www-form-urlencoded;charset=utf-8"
  96. request, err := http.NewRequest("POST", urls, strings.NewReader(formValues.Encode()))
  97. if err != nil {
  98. return
  99. }
  100. for k, v := range header {
  101. request.Header.Add(k, v)
  102. }
  103. response, err := httpClient.Do(request) //前面预处理一些参数,状态,Do执行发送;处理返回结果;Do:发送请求,
  104. if err != nil {
  105. return
  106. }
  107. defer response.Body.Close()
  108. replay, err := ioutil.ReadAll(response.Body)
  109. if err != nil {
  110. log.Println("read reply error:", err)
  111. return
  112. }
  113. log.Println(string(replay))
  114. json.Unmarshal(replay, &data)
  115. return
  116. }