package enc_vehicle import ( "bytes" "encoding/hex" "encoding/json" "fmt" "gd_gateway/common.in/config" "gd_vehicle/common.in/utils" "github.com/tidwall/gjson" "io/ioutil" "net/http" "strings" "time" ) const ( /* APPKEY = "ad076e9268206f674e1ae08b1c75e18c" APPSEC = "2a7d6784b0c70a519b0465edf26b1326" HOST = "http://114.116.149.44:61001" */ APPKEY = "69581375b8dfa08b88de7cbad1d7fca4" APPSEC = "96460526b508f975ab063cdcf730d372" HOST = "http://gdtest.cn" ) func concatGetURL(url string, data map[string]string) string { if data == nil { return url } for k, v := range data { url = url + "&" + k + "=" + v } url = strings.Replace(url, "&", "?", 1) return url } func GdClientPost(api string, data map[string]interface{}) (result []byte, err error) { jsonData, err := json.Marshal(data) if err != nil { return nil, err } var realData = map[string]string{ "data": string(jsonData), } jsonData, err = json.Marshal(realData) if err != nil { return nil, err } client := &http.Client{} client.Timeout = 10 * time.Second req, err := http.NewRequest("POST", api, bytes.NewBuffer(jsonData)) if err != nil { return nil, err } timestamp := "1111" req.Header.Set("app_key", APPKEY) req.Header.Set("ts", timestamp) signtext := fmt.Sprintf("%s%s%s%s", timestamp, string(jsonData), APPKEY, APPSEC) req.Header.Set("sign", utils.MD5(signtext)) req.Header.Set("Content-Type", "application/json") resp, err := client.Do(req) if err != nil { return nil, err } defer resp.Body.Close() result, err = ioutil.ReadAll(resp.Body) return } func GdClientGet(api string, data map[string]interface{}) (result []byte, err error) { jsonData, err := json.Marshal(data) if err != nil { return nil, err } client := &http.Client{} client.Timeout = 10 * time.Second rData := map[string]string{"data": string(jsonData)} req, err := http.NewRequest("GET", concatGetURL(api, rData), nil) if err != nil { return nil, err } timestamp := "1111" req.Header.Set("app_key", APPKEY) req.Header.Set("ts", timestamp) signtext := fmt.Sprintf("%s%s%s%s", timestamp, string(jsonData), APPKEY, APPSEC) req.Header.Set("sign", utils.MD5(signtext)) req.Header.Set("Content-Type", "application/json") resp, err := client.Do(req) if err != nil { return nil, err } defer resp.Body.Close() result, err = ioutil.ReadAll(resp.Body) fmt.Println(string(result), " error: ", err) return } func GdClientCryptoGet(api string, data map[string]interface{}) (result []byte, err error) { jsonData, err := json.Marshal(data) if err != nil { return nil, err } client := &http.Client{} client.Timeout = 10 * time.Second bytes, _ := config.AesEncrypt(string(jsonData), APPSEC) rData := map[string]string{"data": hex.EncodeToString(bytes)} req, err := http.NewRequest("GET", concatGetURL(api, rData), nil) if err != nil { return nil, err } timestamp := "1111" req.Header.Set("app_key", APPKEY) req.Header.Set("ts", timestamp) signtext := fmt.Sprintf("%s%s%s%s", timestamp, hex.EncodeToString(bytes), APPKEY, APPSEC) req.Header.Set("sign", utils.MD5(signtext)) req.Header.Set("Content-Type", "application/json") resp, err := client.Do(req) if err != nil { return nil, err } defer resp.Body.Close() rByte, err := ioutil.ReadAll(resp.Body) fmt.Println(string(rByte), " error: ", err) res := gjson.GetBytes(rByte, "data").String() // fmt.Println(res) result, err = config.AesDecrypt([]byte(res), []byte(APPSEC)) // fmt.Println("err",err) //fmt.Println(string(result)) return }