package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
"time"
)
type PostStruct struct {
CardNo string `json:"cardNo"`
EquipmentNo string `json:"equipmentNo"`
EquipType string `json:"equipType"`
ScreenSpec string `json:"screenSpec"`
}
type ResultToken struct {
Msg string `json:"msg"`
Data Token `json:"data"`
Status string `json:"status"`
}
type Token struct {
Token string `json:"token"`
}
type OnlineRanking struct {
Msg string `json:"msg"`
Data []Ranking `json:"data"`
Status string `json:"status"`
}
type Ranking struct {
MemberPic string `json:"memberPic"`
MonthDistance int `json:"monthDistance"`
Number int `json:"number"`
NickName string `json:"nickName"`
MonthCalorie int `json:"monthCalorie"`
Ranking float32 `json:"ranking"`
EquipmentNo string `json:"equipmentNo"`
MemberId string `json:"memberId"`
//Csdfds string `json:"ccc"`
}
func main() {
postString :=PostStruct{
CardNo:"2955313449",
EquipmentNo:"9",
EquipType:"0",
ScreenSpec:"0",
}
url :="http://192.168.1.6:8777/v1/user/cardLogin"
headerMap :=map[string]string{}
headerMap["Content-Type"] ="application/json;charset=UTF-8"
jsonPostStruct, err :=json.Marshal(postString)
if err !=nil {
fmt.Println("生成json字符串错误")
}
code,result, err :=HttpPost(url, headerMap, strings.NewReader(string(jsonPostStruct)))
fmt.Println("最终返回Code值:", code)
fmt.Println("最终返回值:", result)
var resultToken ResultToken
ppp :=json.Unmarshal([]byte(result), &resultToken)
if ppp ==nil {
fmt.Println("================json str 转struct==")
fmt.Println("resultToken.CardNo:", resultToken.Msg)
fmt.Println("resultToken.Data:", resultToken.Data.Token)
fmt.Println("resultToken.Status:", resultToken.Status)
//fmt.Println(part.SerTcpSocketPort)
}else {
fmt.Println("转换错误", ppp)
}
///////////////
url ="http://192.168.1.6:8777/v1/equipment/onlineRankingList?equipmentNo=9"
newheaderMap :=map[string]string{}
//headerMap["Content-Type"] = "application/json;charset=UTF-8"
newheaderMap["token"] =resultToken.Data.Token
code, resultStr, err :=HttpGet(url, newheaderMap)
var onlineRanking OnlineRanking
pppErr :=json.Unmarshal([]byte(resultStr), &onlineRanking)
if pppErr ==nil {
fmt.Println("================json str 转struct==")
fmt.Println("Msg:", onlineRanking.Msg)
fmt.Println("Number:", onlineRanking.Data[0].Number)
fmt.Println("NickName:", onlineRanking.Data[0].NickName)
fmt.Println("EquipmentNo:", onlineRanking.Data[0].EquipmentNo)
fmt.Println("MemberPic:", onlineRanking.Data[0].MemberPic)
fmt.Printf("Ranking %.2f\n", onlineRanking.Data[0].Ranking)
//fmt.Println(part.SerTcpSocketPort)
}else {
fmt.Println("转换错误", pppErr)
}
testDs()
}
func testDs() {
//"B76FBFD0CAFC11E98EA4B8599F37F0402236"
key :="B76FBFD0CAFC11E98EA4B8599F37F0402236"
url :="http://ds.kismart.com.cn/api/member/login/social"
newheaderMap :=map[string]string{}
newheaderMap["Content-Type"] ="application/x-www-form-urlencoded"
newheaderMap["Accept"] ="application/json"
//application/x-www-form-urlencoded 的传参数
postDataStr :="uniqueId=" +key +"&platform=lddLogin"
//fmt.Println(string(postData))
resultCode, resultStr, err :=HttpPost(url, newheaderMap, strings.NewReader(postDataStr))
fmt.Println(resultCode)
if (err !=nil) {
fmt.Println(err)
}else {
fmt.Println(string(resultStr))
}
}
func httpDo() {
postString :=PostStruct{
CardNo:"2955313449",
EquipmentNo:"9",
EquipType:"0",
ScreenSpec:"0",
}
//postString.CardNo = "2955313449"
//postString.EquipmentNo = "9"
//postString.EquipType = "0"
//postString.ScreenSpec = "0"
//struct 到json str
//{
// "cardNo": "2955313449",
// "equipmentNo": "9",
// "equipType": "0",
// "screenSpec": "0"
//}
jsonPostStruct, err :=json.Marshal(postString)
if err !=nil {
fmt.Println("生成json字符串错误", err)
}
fmt.Println("================struct 到json str==")
fmt.Println(string(jsonPostStruct))
//var jsonStr = []byte(string(jsonPostStruct))
var jsonStr = []byte(`{"cardNo": "2955313449","equipmentNo": "9","equipType": "0","screenSpec": "0"}`)
// var jsonStr = []byte(`{
// "cardNo": "2955313449",
// "equipmentNo": "9",
// "equipType": "0",
// "screenSpec": "0"
//}`)
fmt.Println(string(jsonStr))
//req, err := http.NewRequest("POST",
// "http://192.168.1.6:8777/v1/user/cardLogin",
// bytes.NewBuffer(jsonStr))
req, err :=http.NewRequest("POST",
"http://192.168.1.6:8777/v1/user/cardLogin",
strings.NewReader(string(jsonPostStruct)))
if err !=nil {
// handle error
println(err)
}
req.Header.Set("Content-Type", "application/json;charset=UTF-8")
// req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err :=client.Do(req)
defer resp.Body.Close()
fmt.Println("response Status:", resp.Status)
body, err :=ioutil.ReadAll(resp.Body)
if err !=nil {
// handle error
fmt.Println(err)
}
fmt.Println(string(body))
}
/**
*/
func HttpPost(url string, headerMap map[string]string, read io.Reader) (string, string, error) {
//fmt.Println("url %s : postData %s", url, postData)
req, err :=http.NewRequest("POST", url, read)
// strings.NewReader(postData))
if err !=nil {
// handle error
return "", "", err
}
for k, v :=range headerMap {
fmt.Printf("k:[%v].v:[%v]\n", k, v)// 输出k,v值
req.Header.Set(k, v)
}
// req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
client.Timeout =time.Second *10
resp, err :=client.Do(req)
defer resp.Body.Close()
if err !=nil {
return resp.Status, "", err
}
fmt.Println("response Status:", resp.Status)
body, err :=ioutil.ReadAll(resp.Body)
if err !=nil {
return resp.Status, "", err
}
fmt.Println(string(body))
return resp.Status, string(body), nil
}
func HttpGet(url string, headerMap map[string]string) (string, string, error) {
fmt.Println("url %s : postData %s", url)
req, err :=http.NewRequest("POST", url, nil)
if err !=nil {
return "", "", err
}
for k, v :=range headerMap {
fmt.Printf("k:[%v].v:[%v]\n", k, v)// 输出k,v值
req.Header.Set(k, v)
}
// req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err :=client.Do(req)
defer resp.Body.Close()
if err !=nil {
return resp.Status, "", err
}
fmt.Println("response Status:", resp.Status)
body, err :=ioutil.ReadAll(resp.Body)
if err !=nil {
return resp.Status, "", err
}
fmt.Println(string(body))
return resp.Status, string(body), nil
}