using System;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.IO;using System.Xml;using System.Text;using System.Net;public partial class _Default : System.Web.UI.Page{ const string Token = "token";//你的token protected void Page_Load(object sender, EventArgs e) { string postStr = ""; Valid(); //验证通过后可删除此行代码 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)) { ResponseMsg(postStr); } } } /// <summary> /// 返回信息结果(微信信息返回) /// </summary> /// <param name="weixinXML"></param> private void ResponseMsg(string weixinXML) { //回复消息的部分:你的代码写在这里 XmlDocument doc = new XmlDocument(); doc.LoadXml(weixinXML); XmlNodeList list = doc.GetElementsByTagName("xml"); XmlNode xn = list[0]; string FromUserName = xn.SelectSingleNode("//FromUserName").InnerText; string ToUserName = xn.SelectSingleNode("//ToUserName").InnerText; string content = "",menu=""; content = xn.SelectSingleNode("//Content").InnerText; int i; string[] s = { "查询课表", "查询单词", "开心一刻", "星座运势" }; i = CheckId(content); dbhelp db = new dbhelp(); db.getBuzhuo(FromUserName); if (content.Equals("Hello2BizUser")) { menu = "你好,我是陈家二当家!\n您可以随时输入h获取菜单!"; content = menu; } else if (content.Equals("h") || content.Equals("H")) { for (int ii = 0; ii < s.Length; ii ) { menu = (ii 1).ToString() "、" s[ii].ToString() "\n"; db.setValue(0, 0, 0); } content = menu; } else { switch (CheckId(db.First)) { case 0://从数据库读取到的上一次输入记录为0 switch (i) { case 0: //content = "您没有选择操作!\n" menu; content = getStr(content); content = "\n您可以随时输入h获取菜单!"; break; case 1: content = "您选择的是" s[i - 1] "操作"; content = "\n请输入要查询的日期,如:1-1表示1月1日\n月和日之间用减号分隔开"; content = "\n您可以随时输入h获取菜单!"; db.setValue(i, 0, 0); break; case 2: content = "您选择的是" s[i - 1] "操作"; content = "\n请回复您要查询的单词"; content = "\n您可以随时输入h获取菜单!"; db.setValue(i, 0, 0); break; case 3: content = getJokeFromChong() "\n"; content = "\n继续回复任意内容您可继续欣赏笑话,回复h回到主菜单!"; db.setValue(i, 0, 0); break; case 4: content = "请输入您要查询的星座\n"; content = "\n您可以随时输入h获取菜单!"; db.setValue(i, 0, 0); break; default: content = getStr(content); content = "\n您可以随时输入h获取菜单!"; break; } break; case 1://从数据库读取到的上一次输入记录为1 //content = getClass(content); if (CheckId(content) > 0) { } DateTime dt = Convert.ToDateTime(content); content = getClass(content); content = "\n您可以随时输入h获取菜单!"; db.setValue(0, 0, 0); break; case 2:// 从数据库读取到的上一次输入记录为2 content = Query(content); content = "\n您可以随时输入h获取菜单!"; db.setValue(0, 0, 0); break; case 3:// 从数据库读取到的上一次输入记录为3 content = getJokeFromChong() "\n"; content = "\n继续回复任意内容您可继续欣赏笑话,回复h回到主菜单!"; db.setValue(0, 0, 0); break; case 4: content = getXingzuo(content) "\n"; content = "\n继续回复星座可继续查询,回复h回到主菜单!"; break; default: break; } } string strresponse = "<xml>"; strresponse = strresponse "<ToUserName><![CDATA[" FromUserName "]]></ToUserName>"; strresponse = strresponse "<FromUserName><![CDATA[" ToUserName "]]></FromUserName>"; strresponse = strresponse "<CreateTime>" DateTime.Now.Ticks.ToString() "</CreateTime>"; strresponse = strresponse "<MsgType><![CDATA[text]]></MsgType>"; strresponse = strresponse "<Content><![CDATA[" content "]]></Content>"; strresponse = strresponse "<FuncFlag>0<FuncFlag>"; strresponse = strresponse "</xml>"; WriteLog("postStr:" content); Response.Write(strresponse); } public string getStr(string s) { string str=""; DateTime dt; if (DateTime.TryParse(s, out dt)) { str= getClass(s); } else { string str2 = getXingzuo(s); if (str2.Contains("输入格式")) { str2=getJokeFromChong(); int i = str2.IndexOf("(响应时间") - 1; str2 = str2.Substring(0, i); str = "我猜测您是想看笑话了,错了回H进行重新选择哦~\n" str2; } else { str = str2; } } return str; } /// <summary> /// 获取课程信息信息,返回结果 /// </summary> private string getClass(string key) { DateTime DT = Convert.ToDateTime(key); string dn = DT.DayOfWeek.ToString(), str = ""; dbhelp db = new dbhelp(); DataSet ds = db.volid(dn, DT); foreach (DataRow dr in ds.Tables[0].Rows) { str = classinfo(Convert.ToInt32(dr["classdesc"])) "\n课程:" dr["class"] "\n地点:" dr["room"] "\n授课老师:" dr["teacher"] "\n"; } if (str.Equals("")) { str = "这天没有课哦,开心么?"; } return str; } /// <summary> /// 取得当前时间,判断下一节课是第几节课。 /// </summary> private int classinfo() { int flag = 0; string[] s = { "8:00", "10:00", "14:00", "15:50" }; for (int i = 0; i < s.Length; i ) { DateTime dt1 = Convert.ToDateTime(s[i]); DateTime dt2 = DateTime.Now; if (DateTime.Compare(dt1, dt2) > 0)//1、如果比8:00大,那么接下去就是第一节课, { flag = i 1; break; } } return flag; } private string classinfo(int i) { string[] s = { "8:00-9:40", "10:00-11:40", "14:00-15:40", "15:50-17:30" }; return s[i - 1]; } /// <summary> /// 单词查询,返回结果。 /// </summary> public string Query(string s) { string result = "", trans = "", phonetic = "", web = ""; int flag = 0, i = 0; XmlDocument xd = new XmlDocument(); xd.Load("http://fanyi.youdao.com/openapi.do?keyfrom=microducom&key=1360214796&type=data&doctype=xml&version=1.1&q=" s); XmlNodeList fylist = xd.GetElementsByTagName("basic"); foreach (XmlNode xn1 in fylist) { XmlNode xex = xn1.SelectSingleNode("//explains"); XmlNodeList exs = xex.ChildNodes; foreach (XmlNode ex in exs) { XmlElement xe2 = (XmlElement)ex; trans = xe2.InnerText "\n"; flag = 1; } if (xn1.SelectSingleNode("//phonetic") != null) phonetic = xn1.SelectSingleNode("//phonetic").InnerText; } XmlNodeList weblist = xd.SelectSingleNode("//web").ChildNodes; if (weblist != null) { for (i = 0; i < weblist.Count; i ) { XmlElement xn2 = (XmlElement)weblist[i]; //web = xn2.Value "\n"; if (xn2.HasChildNodes) { XmlNodeList web2 = xn2.ChildNodes; foreach (XmlNode xn3 in web2) { if (xn3.HasChildNodes) { XmlNodeList web3 = xn3.ChildNodes; foreach (XmlNode xn4 in web3) { web = xn4.InnerText "\n"; } } } } } } if (flag == 1) { result = "您要查询的单词:" s "\n发音:" phonetic "\n" "释义:" trans "\n" "\n网络释义:\n" web; } else { result = "对不起,查询失败!"; } return result; } /// <summary> /// 写日志(用于跟踪) /// </summary> private void WriteLog(string strMemo) { if (!Directory.Exists(Server.MapPath(@"logs\"))) { Directory.CreateDirectory(Server.MapPath(@"logs\")); } string filename = Server.MapPath(@"logs/log.txt"); StreamWriter sr = null; try { if (!File.Exists(filename)) { sr = File.CreateText(filename); } else { sr = File.AppendText(filename); } sr.WriteLine(strMemo); } catch { } finally { if (sr != null) sr.Close(); } } public int CheckId(string sid) { int id; try { if (sid == null || sid.ToString() == "") { return 0; } else { id = Convert.ToInt32(sid.ToString()); return id; } } catch { return 0; } } public string getJokeFromChong() { try { WebClient MyWebClient = new WebClient(); MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于对向Internet资源的请求进行身份验证的网络凭据。 Byte[] pageData = MyWebClient.DownloadData("http://wap.unidust.cn/api/searchout.do?type=wap&ch=1001&info=%E7%AC%91%E8%AF%9D"); //从指定网站下载数据 //string pageHtml = Encoding.Default.GetString(pageData); //如果获取网站页面采用的是GB2312,则使用这句 string pageHtml = Encoding.UTF8.GetString(pageData); //如果获取网站页面采用的是UTF-8,则使用这句 //pageHtml = pageHtml.Substring(217); //int i = pageHtml.IndexOf("(响应时间") - 1; //pageHtml = pageHtml.Substring(0, i); return StripHTML(pageHtml).Substring(32); } catch (WebException webEx) { Console.WriteLine(webEx.Message.ToString()); return "没找到笑话哦~~"; } } public string getXingzuo(string s) { try { WebClient MyWebClient = new WebClient(); MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于对向Internet资源的请求进行身份验证的网络凭据。 Byte[] pageData = MyWebClient.DownloadData("http://wap.unidust.cn/api/searchout.do?type=wap&ch=1001&info=" s "&appid=81"); //从指定网站下载数据 //string pageHtml = Encoding.Default.GetString(pageData); //如果获取网站页面采用的是GB2312,则使用这句 string pageHtml = Encoding.UTF8.GetString(pageData); //如果获取网站页面采用的是UTF-8,则使用这句 pageHtml = StripHTML(pageHtml).Substring(24); //int i = pageHtml.IndexOf("(响应时间") - 1; //pageHtml = pageHtml.Substring(0, i); return pageHtml; } catch (WebException webEx) { Console.WriteLine(webEx.Message.ToString()); return "没找到笑话哦~~"; } } public string StripHTML(string source) { try { string result; result = source.Replace("\r", " "); result = result.Replace("\n", " "); result = result.Replace("'", " "); result = result.Replace("\t", string.Empty); result = System.Text.RegularExpressions.Regex.Replace(result, @"( ) ", " "); result = System.Text.RegularExpressions.Regex.Replace(result, @"<( )*head([^>])*>", "<head>", System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = System.Text.RegularExpressions.Regex.Replace(result, @"(<( )*(/)( )*head( )*>)", "</head>", System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = System.Text.RegularExpressions.Regex.Replace(result, "(<head>).*(</head>)", string.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = System.Text.RegularExpressions.Regex.Replace(result, @"<( )*script([^>])*>", "<script>", System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = System.Text.RegularExpressions.Regex.Replace(result, @"(<( )*(/)( )*script( )*>)", "</script>", System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = System.Text.RegularExpressions.Regex.Replace(result, @"(<script>).*(</script>)", string.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = System.Text.RegularExpressions.Regex.Replace(result, @"<( )*style([^>])*>", "<style>", System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = System.Text.RegularExpressions.Regex.Replace(result, @"(<( )*(/)( )*style( )*>)", "</style>", System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = System.Text.RegularExpressions.Regex.Replace(result, "(<style>).*(</style>)", string.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = System.Text.RegularExpressions.Regex.Replace(result, @"<( )*td([^>])*>", "\t", System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = System.Text.RegularExpressions.Regex.Replace(result, @"<( )*br( )*>", "\r", System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = System.Text.RegularExpressions.Regex.Replace(result, @"<( )*li( )*>", "\r", System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = System.Text.RegularExpressions.Regex.Replace(result, @"<( )*div([^>])*>", "\r\r", System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = System.Text.RegularExpressions.Regex.Replace(result, @"<( )*tr([^>])*>", "\r\r", System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = System.Text.RegularExpressions.Regex.Replace(result, @"<( )*p([^>])*>", "\r\r", System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = System.Text.RegularExpressions.Regex.Replace(result, @"<[^>]*>", string.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = System.Text.RegularExpressions.Regex.Replace(result, @" ", " ", System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = System.Text.RegularExpressions.Regex.Replace(result, @"•", " * ", System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = System.Text.RegularExpressions.Regex.Replace(result, @"‹", "<", System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = System.Text.RegularExpressions.Regex.Replace(result, @"›", ">", System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = System.Text.RegularExpressions.Regex.Replace(result, @"™", "(tm)", System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = System.Text.RegularExpressions.Regex.Replace(result, @"⁄", "/", System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = System.Text.RegularExpressions.Regex.Replace(result, @"<", "<", System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = System.Text.RegularExpressions.Regex.Replace(result, @">", ">", System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = System.Text.RegularExpressions.Regex.Replace(result, @"©", "(c)", System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = System.Text.RegularExpressions.Regex.Replace(result, @"®", "(r)", System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = System.Text.RegularExpressions.Regex.Replace(result, @"&(.{2,6});", string.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = result.Replace("\n", "\r"); result = System.Text.RegularExpressions.Regex.Replace(result, "(\r)( ) (\r)", "\r\r", System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = System.Text.RegularExpressions.Regex.Replace(result, "(\t)( ) (\t)", "\t\t", System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = System.Text.RegularExpressions.Regex.Replace(result, "(\t)( ) (\r)", "\t\r", System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = System.Text.RegularExpressions.Regex.Replace(result, "(\r)( ) (\t)", "\r\t", System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = System.Text.RegularExpressions.Regex.Replace(result, "(\r)(\t) (\r)", "\r\r", System.Text.RegularExpressions.RegexOptions.IgnoreCase); result = System.Text.RegularExpressions.Regex.Replace(result, "(\r)(\t) ", "\r\t", System.Text.RegularExpressions.RegexOptions.IgnoreCase); string breaks = "\r\r\r"; string tabs = "\t\t\t\t\t"; for (int index = 0; index < result.Length; index ) { result = result.Replace(breaks, "\r\r"); result = result.Replace(tabs, "\t\t\t\t"); breaks = breaks "\r"; tabs = tabs "\t"; } return result; } catch { //MessageBox.Show("Error"); return source; } } /// <summary> /// 验证微信签名 /// </summary> /// * 将token、timestamp、nonce三个参数进行字典序排序 /// * 将三个参数字符串拼接成一个字符串进行sha1加密 /// * 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。 /// <returns></returns> private bool CheckSignature() { string signature = Request.QueryString["signature"].ToString(); string timestamp = Request.QueryString["timestamp"].ToString(); string nonce = Request.QueryString["nonce"].ToString(); string[] ArrTmp = { Token, timestamp, nonce }; Array.Sort(ArrTmp); //字典排序 string tmpStr = string.Join("", ArrTmp); tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1"); tmpStr = tmpStr.ToLower(); if (tmpStr.Equals(signature)) { return true; } else { return false; } } private void Valid() { string echoStr = Request.QueryString["echoStr"].ToString(); if (CheckSignature()) { if (!string.IsNullOrEmpty(echoStr)) { Response.Write(echoStr); Response.End(); } } }}
下载asp.net 响应微信公众号调用 例子用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论