<%@ WebHandler Language="C#" Class="ce" %> using System; using System.Web; using System.Net; using System.Text; using System.IO; using System.Collections.Generic; using LitJson; using System.Web.Script.Serialization; public class ce : IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; //微信access_tocke string ulr = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" + this.access_token(); HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create(ulr); //创建url request.Method = "POST"; request.ContentType = "application/json;charset=UTF-8"; JsonData data = new JsonData(); data["path"] = "\'pages/index?query=1"; data["width"] = "430"; string json = data.ToJson(); byte[] load = Encoding.UTF8.GetBytes(json); request.ContentLength = load.Length; Stream writer = request.GetRequestStream(); writer.Write(load,0,load.Length); HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse(); Stream s = response.GetResponseStream(); byte[] mg = StreamToBytes(s); context.Response.BinaryWrite(mg); } public string access_token() { string urls = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=1111&secret=1111"; HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create(urls); //请求api HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse(); string respText = ""; using (Stream resStream = response.GetResponseStream()) { StreamReader reader = new StreamReader(resStream, Encoding.Default); respText = reader.ReadToEnd(); resStream.Close(); } JavaScriptSerializer Jss = new JavaScriptSerializer(); Dictionary<string, object> respDic = (Dictionary<string, object>)Jss.DeserializeObject(respText); //通过键access_token获取值 return respDic["access_token"].ToString(); } public static byte[] StreamToBytes(Stream stream) { List<byte> bytes = new List<byte>(); int temp = stream.ReadByte(); while (temp != -1) { bytes.Add((byte)temp); temp = stream.ReadByte(); } return bytes.ToArray(); } public bool IsReusable { get { return false; } } }
请发表评论