|
@@ -7,23 +7,29 @@ import (
|
|
"fmt"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"io/ioutil"
|
|
"log"
|
|
"log"
|
|
|
|
+ "net"
|
|
"net/http"
|
|
"net/http"
|
|
|
|
+ "net/url"
|
|
"qfw/util"
|
|
"qfw/util"
|
|
"strings"
|
|
"strings"
|
|
"time"
|
|
"time"
|
|
)
|
|
)
|
|
|
|
|
|
var (
|
|
var (
|
|
- config map[string]string
|
|
|
|
- // appid = "jyNjdXQgUDAwdaTklMPz5i"
|
|
|
|
- // key = "404M0v2j"
|
|
|
|
- // apiurl = "http://127.0.0.1:8801"
|
|
|
|
- // appid = "jyMi1XQgMABQNcSkBMIhBq"
|
|
|
|
- // key = "6PzV0CUa"
|
|
|
|
- // apiurl = "https://testapi.jianyu360.com"
|
|
|
|
|
|
+ config map[string]string
|
|
appid, key, apiurl, day string
|
|
appid, key, apiurl, day string
|
|
|
|
+ httpClient *http.Client
|
|
|
|
+ MaxIdleCons int = 100
|
|
|
|
+ MaxIdleConsPerHost int = 100
|
|
|
|
+ IdleConnTimeout int = 2048
|
|
|
|
+ ConnectTimeOut int = 30
|
|
|
|
+ KeepAlive int = 30
|
|
)
|
|
)
|
|
|
|
|
|
|
|
+func init() {
|
|
|
|
+ httpClient = createHttpClient()
|
|
|
|
+}
|
|
|
|
+
|
|
func main() {
|
|
func main() {
|
|
util.ReadConfig(&config)
|
|
util.ReadConfig(&config)
|
|
appid = config["appid"]
|
|
appid = config["appid"]
|
|
@@ -33,6 +39,23 @@ func main() {
|
|
getData()
|
|
getData()
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func createHttpClient() *http.Client {
|
|
|
|
+ client := &http.Client{
|
|
|
|
+ Transport: &http.Transport{
|
|
|
|
+ Proxy: http.ProxyFromEnvironment,
|
|
|
|
+ DialContext: (&net.Dialer{
|
|
|
|
+ Timeout: time.Duration(ConnectTimeOut) * time.Second, //TCP连接超时30s
|
|
|
|
+ KeepAlive: time.Duration(KeepAlive) * time.Second, //TCP keepalive保活检测定时30s
|
|
|
|
+ }).DialContext,
|
|
|
|
+ MaxIdleConns: MaxIdleCons,
|
|
|
|
+ MaxIdleConnsPerHost: MaxIdleConsPerHost,
|
|
|
|
+ IdleConnTimeout: time.Duration(IdleConnTimeout) * time.Second, //闲置连接超时2048s
|
|
|
|
+ ResponseHeaderTimeout: time.Second * 60,
|
|
|
|
+ },
|
|
|
|
+ }
|
|
|
|
+ return client
|
|
|
|
+}
|
|
|
|
+
|
|
func getToken() (token string) {
|
|
func getToken() (token string) {
|
|
tm := fmt.Sprintf("%d", time.Now().Unix())
|
|
tm := fmt.Sprintf("%d", time.Now().Unix())
|
|
res := post(apiurl+"/user/access_token", map[string]string{
|
|
res := post(apiurl+"/user/access_token", map[string]string{
|
|
@@ -40,7 +63,7 @@ func getToken() (token string) {
|
|
"timestamp": tm,
|
|
"timestamp": tm,
|
|
"signature": MD5(appid + tm + key),
|
|
"signature": MD5(appid + tm + key),
|
|
//"key": "6PzV0CUa",
|
|
//"key": "6PzV0CUa",
|
|
- })
|
|
|
|
|
|
+ }, nil)
|
|
log.Println(tm, MD5(appid+tm+key), res)
|
|
log.Println(tm, MD5(appid+tm+key), res)
|
|
if res != nil && res["access_token"] != "" {
|
|
if res != nil && res["access_token"] != "" {
|
|
token, _ = res["access_token"].(string)
|
|
token, _ = res["access_token"].(string)
|
|
@@ -49,15 +72,21 @@ func getToken() (token string) {
|
|
}
|
|
}
|
|
|
|
|
|
func getData() {
|
|
func getData() {
|
|
- token := getToken()
|
|
|
|
|
|
+ // token := getToken()
|
|
|
|
+ tm := fmt.Sprint(time.Now().Unix())
|
|
data := post(apiurl+"/data/getalldata", map[string]string{
|
|
data := post(apiurl+"/data/getalldata", map[string]string{
|
|
- "access_token": token,
|
|
|
|
- "day": day,
|
|
|
|
- "next": "92",
|
|
|
|
|
|
+ // "access_token": token,
|
|
|
|
+ "day": day,
|
|
|
|
+ "next": "0",
|
|
|
|
+ "appid": appid,
|
|
|
|
+ }, map[string]string{
|
|
|
|
+ "timestamp": tm,
|
|
|
|
+ "signature": MD5(appid + tm + key),
|
|
})
|
|
})
|
|
//s, _ := json.Marshal(data["data"])
|
|
//s, _ := json.Marshal(data["data"])
|
|
//delete(data, "data")
|
|
//delete(data, "data")
|
|
- log.Println(token, data)
|
|
|
|
|
|
+ log.Println("tm", appid, tm)
|
|
|
|
+ log.Println(MD5(appid+tm+key), data)
|
|
}
|
|
}
|
|
|
|
|
|
func MD5(str string) string {
|
|
func MD5(str string) string {
|
|
@@ -66,19 +95,31 @@ func MD5(str string) string {
|
|
return strings.ToUpper(hex.EncodeToString(h.Sum(nil)))
|
|
return strings.ToUpper(hex.EncodeToString(h.Sum(nil)))
|
|
}
|
|
}
|
|
|
|
|
|
-func post(url string, form map[string]string) (data map[string]interface{}) {
|
|
|
|
- str := ""
|
|
|
|
|
|
+func post(urls string, form, header map[string]string) (data map[string]interface{}) {
|
|
|
|
+ formValues := make(url.Values)
|
|
for k, v := range form {
|
|
for k, v := range form {
|
|
- str += "&" + k + "=" + v
|
|
|
|
|
|
+ formValues[k] = []string{v}
|
|
|
|
+ }
|
|
|
|
+ log.Println(formValues)
|
|
|
|
+ header["Content-Type"] = "application/x-www-form-urlencoded;charset=utf-8"
|
|
|
|
+ request, err := http.NewRequest("POST", urls, strings.NewReader(formValues.Encode()))
|
|
|
|
+ if err != nil {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ for k, v := range header {
|
|
|
|
+ request.Header.Add(k, v)
|
|
|
|
+ }
|
|
|
|
+ response, err := httpClient.Do(request) //前面预处理一些参数,状态,Do执行发送;处理返回结果;Do:发送请求,
|
|
|
|
+ if err != nil {
|
|
|
|
+ return
|
|
}
|
|
}
|
|
- log.Println(str)
|
|
|
|
- res, err := http.Post(url, "application/x-www-form-urlencoded", strings.NewReader(str))
|
|
|
|
|
|
+ defer response.Body.Close()
|
|
|
|
+ replay, err := ioutil.ReadAll(response.Body)
|
|
if err != nil {
|
|
if err != nil {
|
|
- log.Println("post err:", err.Error())
|
|
|
|
- } else if res.Body != nil {
|
|
|
|
- defer res.Body.Close()
|
|
|
|
- bs, _ := ioutil.ReadAll(res.Body)
|
|
|
|
- json.Unmarshal(bs, &data)
|
|
|
|
|
|
+ log.Println("read reply error:", err)
|
|
|
|
+ return
|
|
}
|
|
}
|
|
|
|
+ log.Println(string(replay))
|
|
|
|
+ json.Unmarshal(replay, &data)
|
|
return
|
|
return
|
|
}
|
|
}
|