/// <summary> /// 获取ticket /// </summary> /// <param name="access_token"></param> /// <returns></returns> public static string getTicket(string access_token2) { BLL.CarShowBLL _bll = new BLL.CarShowBLL(); string ticket = null; ticket = _bll.GetToken(1); if (ticket == string.Empty) { string url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + access_token2 + "&type=jsapi";//这个url链接和参数不能变 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.Method = "GET"; HttpWebResponse response = request.GetResponse() as HttpWebResponse; Stream ioStream = response.GetResponseStream(); StreamReader sr = new StreamReader(ioStream, Encoding.UTF8); string html = sr.ReadToEnd(); sr.Close(); ioStream.Close(); response.Close(); string key = "\"ticket\":\""; int startIndex = html.IndexOf(key); if (startIndex != -1) { int endIndex = html.IndexOf("\",", startIndex); string openid = html.Substring(startIndex + key.Length, endIndex - startIndex - key.Length); //MyOpenId.Value=openid; ticket = openid; int instrs = _bll.InsertToken(ticket, 1); } } return(ticket); }
/// <summary> /// 获取token /// </summary> /// <returns></returns> public static string getAccessToken() { string AccessToken = string.Empty; BLL.CarShowBLL _bll = new BLL.CarShowBLL(); AccessToken = _bll.GetToken(0); if (AccessToken == string.Empty) { string grant_type = "client_credential"; //获取access_token填写client_credential string AppId = "wx4c43cea86a410955"; //第三方用户唯一凭证 string secret = "b01e99aa941df6ee9127f68fdf133d86"; //第三方用户唯一凭证密钥,即appsecret //这个url链接地址和参数皆不能变 string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=" + grant_type + "&appid=" + AppId + "&secret=" + secret; //访问链接 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.Method = "GET"; HttpWebResponse response = request.GetResponse() as HttpWebResponse; Stream ioStream = response.GetResponseStream(); StreamReader sr = new StreamReader(ioStream, Encoding.UTF8); string html = sr.ReadToEnd(); sr.Close(); ioStream.Close(); response.Close(); string key = "\"access_token\":\""; int startIndex = html.IndexOf(key); if (startIndex != -1) { int endIndex = html.IndexOf("\",", startIndex); string openid = html.Substring(startIndex + key.Length, endIndex - startIndex - key.Length); //MyOpenId.Value=openid; AccessToken = openid; int instrs = _bll.InsertToken(AccessToken, 0); } } return(AccessToken); }