在微信接口文档中,已经提供的获取微信公众号所绑定的设备。获取微信设备
同时还要附带JSON数据。
所以,关键点在于发送POST请求的时候附带JSON数据
以下是获取所有绑定到公众号的设备。
string token = IsExistAccess_Token();//获取token string strUrl = "https://api.weixin.qq.com/shakearound/device/search?access_token=" + token +"&type=" + 2 + "&last_seen=" + 10097 + "&count=" + 50; LogTool.normalLog(2, "APIUrl为:" + strUrl);//填写日志 string param = "{\"type\": 2,\"last_seen\": 0,\"count\": 3}";// \'\\'转义符 HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(strUrl); req.Method = "POST"; req.ContentType = "application/json;charset=UTF-8"; string content = string.Empty; byte[] byteData = Encoding.UTF8.GetBytes(param); int length = byteData.Length; using (Stream writer = req.GetRequestStream()) { writer.Write(byteData, 0, length); } using (WebResponse wr = req.GetResponse()) { HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse(); StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); content = reader.ReadToEnd();//在这里获取到请求的JSON数据 LogTool.normalLog(2, "获取的设备Json数据为:" + content); }
关键点是下面这个,会在提供的链接后面加上JSON数据
string content = string.Empty; byte[] byteData = Encoding.UTF8.GetBytes(param); int length = byteData.Length; using (Stream writer = req.GetRequestStream()) { writer.Write(byteData, 0, length); }
加上之后,整个链接就变成为:
https://api.weixin.qq.com/shakearound/device/search?access_token=token&type=2&last_seen=10097&count=50
发送这个请求连接之后,会获取到绑定的json结构
{ "data":{ "devices":[ { "comment":"HSBeacon测试", "device_id":23850949, "last_active_time":0, "major":10198, "minor":53984, "poi_id":0, "status":1, "uuid":"FDA50693-A4E2-4FB1-AFCF-C6EB07647825" }, { "comment":"HSBeacon02", "device_id":23850945, "last_active_time":0, "major":10198, "minor":53984, "poi_id":0, "status":1, "uuid":"FDA50693-A4E2-4FB1-AFCF-C6EB07647827" } ], "total_count":1 }, "errcode":0, "errmsg":"success." }
然后进行解析,就能获取到所有的设备信息。