/* * access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token。开发者需要进行妥善保存。 * access_token的存储至少要保留512个字符空间。 * access_token的有效期目前为2个小时,需定时刷新,重复获取将导致上次获取的access_token失效。 */ /// <summary> /// 调用接口,获取access_token /// </summary> public static void RequestAccessToken() { string url = new LinkManage().GetAccessToken(); try { string result = NetHelper.Get(url); Access_TokenData _data = result.JsonDeserialezer <Access_TokenData>(); if (string.IsNullOrEmpty(_data.access_token) == false) { //获取成功 AccessToken = _data.access_token; NextTime = DateTime.Now.AddSeconds(_data.expires_in - 10); //测试代码 LogOperate.Write("获取access_token成功:" + AccessToken); } else { throw new Exception("反序列化Access_TokenData对象失败:" + result); } } catch (Exception ex) { LogOperate.Write("获取AccessToken失败," + ex.Message); if ((DateTime.Now - NextTime).TotalMinutes >= 10) { EnableRun = false; } NextTime = DateTime.Now; } }
/// <summary> /// 获取jsapi_ticket /// 1.有效期为7200秒 /// </summary> public static void RequestJsApi_Ticket() { try { string url = new LinkManage().GetJsApi_Ticket(); string result = NetHelper.Get(url); JObject obj = JObject.Parse(result); try { string ticket = (string)obj["ticket"]; if (string.IsNullOrEmpty(ticket) == false) { Jsapi_Ticket = ticket; return; } throw new Exception("获取ticket为空"); } catch (Exception ex) { throw new Exception("反序列化获取jsapi_ticket失败," + ex.Message + ",返回内容:" + result); } } catch (Exception ex) { LogOperate.Write("获取jsapi_ticket失败," + ex.Message); if ((DateTime.Now - NextTime).TotalMinutes >= 10) { EnableRun = false; } NextTime = DateTime.Now; } }
///// <summary> ///// 接收服务器消息 ///// </summary> //public void ReceiveData() //{ // HttpRequest req = HttpContext.Current.Request; // if (req.HttpMethod == "POST") // { // Stream stream = req.InputStream; // StreamReader reader = new StreamReader(stream, Encoding.UTF8); // try // { // string result = reader.ReadToEnd(); // //1,保存当前消息 // ReceiveMessage _receive = new ReceiveMessage(result); // MsgRecord record = _receive.GetCurrent(); // //2.根据输入内容相应消息 // SendMessage _send = new SendMessage(record); // _send.SendText("您输入发送的内容是:" + record.Content); // LogOperate.Write(result); // } // catch (Exception ex) // { // LogOperate.Write("响应出错:" + ex.Message); // } // finally // { // reader.Close(); // stream.Close(); // } // } //} /// <summary> /// 获取微信服务器ip列表 /// </summary> /// <returns></returns> public WXIPData GetIPList() { try { string url = new LinkManage().GetWXIP(); string result = NetHelper.Get(url); WXIPData _data = result.JsonDeserialezer <WXIPData>(); if (_data == null) { throw new Exception(result); } return(_data); } catch (Exception ex) { throw new Exception("获取微信服务器ip列表异常," + ex.Message); } }