public static NGJSApiTicket GetJSApi_Ticket() { string access_token = NGAccessTokenTools.GetExistAccess_Token(); string strUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + access_token + "&type=jsapi"; NGJSApiTicket mode = new NGJSApiTicket(); 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(); Log.Debug("NGJSApiTicketTools", "GetJSApi_Ticket response : " + content); //Response.Write(content); //在这里对SApiTicket 赋值 NGJSApiTicket token = new NGJSApiTicket(); token = NGJSONHelper.ParseFromJson <NGJSApiTicket>(content); mode.ticket = token.ticket; mode.expires_in = token.expires_in; } return(mode); }
public static string GetExistJSApi_Ticket() { string Token = string.Empty; DateTime YouXRQ; // 读取XML文件中的数据,并显示出来 ,注意文件路径 string filepath = HttpContext.Current.Server.MapPath("NGJSApiTicketMode.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; NGJSApiTicket mode = GetJSApi_Ticket(); xml.SelectSingleNode("xml").SelectSingleNode("Access_Token").InnerText = mode.ticket; _youxrq = _youxrq.AddSeconds(int.Parse(mode.expires_in)); xml.SelectSingleNode("xml").SelectSingleNode("Access_YouXRQ").InnerText = _youxrq.ToString(); xml.Save(filepath); Token = mode.ticket; } return(Token); }