/// <summary> /// 通过appID和appsecret获取Access_token /// </summary> /// <returns></returns> private WXAccess_token GetAccess_token(string appid, string secret) { string strUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret; WXAccess_token mode = new WXAccess_token(); try { HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(strUrl); req.Method = "GET"; using (WebResponse wr = req.GetResponse()) { HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse(); StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); string content = reader.ReadToEnd();//在这里对Access_token 赋值 SeatManage.SeatManageComm.WriteLog.Write(content); //mode = Newtonsoft.Json.JsonConvert.DeserializeObject<WXAccess_token>(content); content = content.Replace("{", "").Replace("}", "").Replace(",", ":").Replace("\"", "");; string[] ar = content.Split(':'); mode.Token = ar[1].ToString(); //SeatManage.SeatManageComm.WriteLog.Write("new content:" + content); mode.expires_in = int.Parse(ar[3].ToString()); SeatManage.SeatManageComm.WriteLog.Write("" + mode.Token + "&&" + mode.expires_in + ""); } } catch (Exception ex) { SeatManage.SeatManageComm.WriteLog.Write(ex.ToString()); throw; } return(mode); }
/// <summary> /// 获取Access_token值 /// </summary> /// <returns></returns> public string IsExistAccess_Token(string appid, string secret) { string Token = string.Empty; try { DateTime YouXRQ; // 读取XML文件中的数据,并显示出来 ,注意文件路径 string filepath = System.Web.HttpContext.Current.Server.MapPath("XMLFile.xml"); StreamReader str = new StreamReader(filepath, System.Text.Encoding.UTF8); XmlDocument xml = new XmlDocument(); xml.Load(str); str.Close(); str.Dispose(); Token = xml.SelectSingleNode("xml").SelectSingleNode("Access_Token").InnerText; YouXRQ = Convert.ToDateTime(xml.SelectSingleNode("xml").SelectSingleNode("Access_YouXRQ").InnerText); if (DateTime.Now > YouXRQ) { DateTime _youxrq = DateTime.Now; WXAccess_token mode = GetAccess_token(appid, secret); xml.SelectSingleNode("xml").SelectSingleNode("Access_Token").InnerText = mode.Token; _youxrq = _youxrq.AddSeconds(mode.expires_in); xml.SelectSingleNode("xml").SelectSingleNode("Access_YouXRQ").InnerText = _youxrq.ToString(); xml.Save(filepath); Token = mode.Token; } } catch (Exception ex) { SeatManage.SeatManageComm.WriteLog.Write(ex.ToString()); throw; } return(Token); }