C#.net 微信公众账号接口开发
编程学习 2021-07-04 22:41www.dzhlxh.cn编程入门
这篇文章主要介绍了C#.net 微信公众账号接口开发,需要的朋友可以参考下
微信越来越火,微信公众平台成为开发成新宠,本文用C#.net开发微信公众信号接口。
微信接口地址代码:
weixin _wx = new weixin(); string postStr = ""; if (Request.HttpMethod.ToLower() == "post") { Stream s = System.Web.HttpContext.Current.Request.InputStream; byte[] b = new byte[s.Length]; s.Read(b, 0, (int)s.Length); postStr = Encoding.UTF8.GetString(b); if (!string.IsNullOrEmpty(postStr)) //请求处理 { _wx.Handle(postStr); } } else { _wx.Auth(); } 微信处理类: public class weixin { private string Token = "weixin_token"; //换成自己的token public void Auth() { string echoStr = System.Web.HttpContext.Current.Request.QueryString["echoStr"]; if (CheckSignature()) { if (!string.IsNullOrEmpty(echoStr)) { System.Web.HttpContext.Current.Response.Write(echoStr); System.Web.HttpContext.Current.Response.End(); } } } public void Handle(string postStr) { //封装请求类 XmlDocument doc = new XmlDocument(); doc.LoadXml(postStr); XmlElement rootElement = doc.DocumentElement; XmlNode MsgType = rootElement.SelectSingleNode("MsgType"); RequestXML requestXML = new RequestXML(); requestXML.ToUserName = rootElement.SelectSingleNode("ToUserName").InnerText; requestXML.FromUserName = rootElement.SelectSingleNode("FromUserName").InnerText; requestXML.CreateTime = rootElement.SelectSingleNode("CreateTime").InnerText; requestXML.MsgType = MsgType.InnerText; if (requestXML.MsgType == "text") { requestXML.Content = rootElement.SelectSingleNode("Content").InnerText; } else if (requestXML.MsgType == "location") { requestXML.Location_X = rootElement.SelectSingleNode("Location_X").InnerText; requestXML.Location_Y = rootElement.SelectSingleNode("Location_Y").InnerText; requestXML.Scale = rootElement.SelectSingleNode("Scale").InnerText; requestXML.Label = rootElement.SelectSingleNode("Label").InnerText; } else if (requestXML.MsgType == "image") { requestXML.PicUrl = rootElement.SelectSingleNode("PicUrl").InnerText; } //回复消息 ResponseMsg(requestXML); } /// <summary> /// 验证微信签名 /// </summary> /// * 将token、timestamp、nonce三个参数进行字典序排序 /// * 将三个参数字符串拼接成一个字符串进行sha1加密 /// * 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。 /// <returns></returns> private bool CheckSignature() { string signature = System.Web.HttpContext.Current.Request.QueryString["signature"]; string timestamp = System.Web.HttpContext.Current.Request.QueryString["timestamp"]; string nonce = System.Web.HttpContext.Current.Request.QueryString["nonce"]; string[] ArrTmp = { Token, timestamp, nonce }; Array.Sort(ArrTmp); //字典排序 string tmpStr = string.Join("", ArrTmp); tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1"); tmpStr = tmpStr.ToLower(); if (tmpStr == signature) { return true; } else { return false; } } /// <summary> /// 回复消息(微信信息返回) /// </summary> /// <param name="weixinXML"></param> private void ResponseMsg(RequestXML requestXML) { try { string resxml = ""; mijiya mi = new mijiya(requestXML.Content,requestXML.FromUserName); if (requestXML.MsgType == "text") { //在这里执行一系列操作,从而实现自动回复内容. string _reMsg = mi.GetReMsg(); if (mi.msgType == 1) { resxml = "<xml><ToUserName><![CDATA[" + requestXML.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + requestXML.ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[news]]></MsgType><Content><![CDATA[]]></Content><ArticleCount>2</ArticleCount><Articles>"; resxml += mi.GetRePic(requestXML.FromUserName); resxml += "</Articles><FuncFlag>1</FuncFlag></xml>"; } else { resxml = "<xml><ToUserName><![CDATA[" + requestXML.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + requestXML.ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[" + _reMsg + "]]></Content><FuncFlag>1</FuncFlag></xml>"; } } else if (requestXML.MsgType == "location") { string city = GetMapInfo(requestXML.Location_X, requestXML.Location_Y); if (city == "0") { resxml = "<xml><ToUserName><![CDATA[" + requestXML.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + requestXML.ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[好啦,我们知道您的位置啦。您可以:" + mi.GetDefault() + "]]></Content><FuncFlag>1</FuncFlag></xml>"; } else { resxml = "<xml><ToUserName><![CDATA[" + requestXML.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + requestXML.ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[好啦,我们知道您的位置啦。您可以:" + mi.GetDefault() + "]]></Content><FuncFlag>1</FuncFlag></xml>"; } } else if (requestXML.MsgType == "image") { resxml = "<xml><ToUserName><![CDATA[" + requestXML.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + requestXML.ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[亲,我没有看懂你的意思。您可以:" + mi.GetDefault() + "]]></Content><FuncFlag>1</FuncFlag></xml>"; //返回10以内条 //int size = 10; //resxml = "<xml><ToUserName><![CDATA[" + requestXML.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + requestXML.ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[news]]></MsgType><Content><![CDATA[]]></Content><ArticleCount>" + size + "</ArticleCount><Articles>"; //List<string> list = new List<string>(); ////假如有20条查询的返回结果 //for (int i = 0; i < 20; i++) //{ // list.Add("1"); /