/// <summary> /// singature签名的生成比较麻烦。 /// 首先生成获取access_token /// (有效期7200秒,开发者必须在自己的服务全局缓存access_token) /// </summary> /// <returns></returns> public static string Getaccesstoken() { string appid = PayConfig.WxAppid(); string secret = PayConfig.WxAppSecret(); string urljson = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret; string strjson = ""; UTF8Encoding encoding = new UTF8Encoding(); HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(urljson); myRequest.Method = "GET"; myRequest.ContentType = "application/x-www-form-urlencoded"; HttpWebResponse response; Stream responseStream; StreamReader reader; string srcString; response = myRequest.GetResponse() as HttpWebResponse; responseStream = response.GetResponseStream(); reader = new System.IO.StreamReader(responseStream, Encoding.UTF8); srcString = reader.ReadToEnd(); reader.Close(); if (srcString.Contains("access_token")) { //CommonJsonModel model = new CommonJsonModel(srcString); CommonJsonModel model = new CommonJsonModel(srcString); strjson = model.GetValue("access_token"); SessionTools.SetSession("access_tokenzj", strjson); } return(strjson); }
/// <summary> /// 接着获取jsapi_ticket: /// 用第一步拿到的access_token 采用http GET方式请求获得jsapi_ticket /// (有效期7200秒,开发者必须在自己的服务全局缓存jsapi_ticket) /// </summary> /// <returns></returns> public static string Getjsapi_ticket() { string accesstoken = (string)SessionTools.GetSession("access_tokenzj"); string urljson = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + accesstoken + "&type=jsapi"; string strjson = ""; UTF8Encoding encoding = new UTF8Encoding(); HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(urljson); myRequest.Method = "GET"; myRequest.ContentType = "application/x-www-form-urlencoded"; HttpWebResponse response = myRequest.GetResponse() as HttpWebResponse; Stream responseStream = response.GetResponseStream(); StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.UTF8); string srcString = reader.ReadToEnd(); reader.Close(); if (srcString.Contains("ticket")) { CommonJsonModel model = new CommonJsonModel(srcString); strjson = model.GetValue("ticket"); SessionTools.SetSession("ticketzj", strjson); } return(strjson); }