代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C#
】
微信公众平台开发C#版
作者:
德帅
/ 发布于
2016/9/14
/
923
//获取请求 protected void Page_Load(object sender, EventArgs e) { string requestMethod = Request.HttpMethod.ToLower(); string responseMsg = ""; switch (requestMethod) { case "get": responseMsg = GetRequest(); break; case "post": responseMsg = PostRequest(); break; default: break; } ResponseWrite(responseMsg); } /// <summary> /// 处理Get请求,用于验证URL的有效性 /// </summary> /// <returns></returns> private string GetRequest() { string msg = ""; // 微信加密签名 string signature = Request.QueryString["signature"]; // 时间戳 string timestamp = Request.QueryString["timestamp"]; // 随机数 string nonce = Request.QueryString["nonce"]; // 随机字符串 string echostr = Request.QueryString["echostr"]; if (string.IsNullOrWhiteSpace(signature)) { msg = "signature为空"; return msg; } bool flag = IsValid(token, signature, timestamp, nonce); if (flag) { msg = echostr; } return msg; } /// <summary> /// 验证是否有效 /// </summary> /// <param name="token"></param> /// <param name="signature"></param> /// <param name="timestamp"></param> /// <param name="nonce"></param> /// <returns></returns> public bool IsValid(string token, string signature, string timestamp, string nonce) { string[] strs = new[] { token, timestamp, nonce }; Array.Sort(strs);// 将token、timestamp、nonce三个参数进行字典序排序 StringBuilder strBul = new StringBuilder();// 将三个参数字符串拼接成一个字符串 foreach (string s in strs) { strBul.Append(s); } //sha1加密 string encryptStr = FormsAuthentication.HashPasswordForStoringInConfigFile(strBul.ToString(), "SHA1"); return encryptStr.ToLower() == signature.ToLower(); } /// <summary> /// 处理用户请求 /// </summary> /// <returns></returns> private string PostRequest() { string content = ""; try { StreamReader reader = new StreamReader(Request.InputStream); string data = reader.ReadToEnd(); if (string.IsNullOrWhiteSpace(data)) { return content; } XmlDocument xml = new XmlDocument(); xml.LoadXml(data); string toUserName = xml.GetElementsByTagName("FromUserName")[0].InnerText; string fromUserName = xml.GetElementsByTagName("ToUserName")[0].InnerText; string msgType = xml.GetElementsByTagName("MsgType")[0].InnerText; int createTime = ConvertDateTimeInt(DateTime.Now); switch (msgType) { case "text": string keyword = xml.GetElementsByTagName("Content")[0].InnerText; content = GameInfo(toUserName, fromUserName, createTime, keyword); break; case "event": string eventType = xml.GetElementsByTagName("Event")[0].InnerText; content = EventHandler(toUserName, fromUserName, createTime, eventType); break; default: string ret = noSeekTips; content = DefaultInfo(toUserName, fromUserName, createTime, ret); break; } } catch (Exception ex) { LogErr.WirteErrToFile(ex, ex.StackTrace, ex.Source); } return content; } /// <summary> /// 事件处理 /// </summary> /// <param name="toUserName"></param> /// <param name="fromUserName"></param> /// <param name="createTime"></param> /// <param name="eventType"></param> /// <returns></returns> private string EventHandler(string toUserName, string fromUserName, int createTime, string eventType) { string msg = ""; switch (eventType) { case "subscribe": msg = AddAttention(toUserName, fromUserName, createTime); break; case "unsubscribe": msg = ""; break; default: msg = ""; break; } return msg; } /// <summary> /// 关注事件 /// </summary> /// <param name="toUserName"></param> /// <param name="fromUserName"></param> /// <param name="createTime"></param> /// <returns></returns> private string AddAttention(string toUserName, string fromUserName, int createTime) { string content = greetings; return DefaultInfo(toUserName, fromUserName, createTime, content); } /// <summary> /// 默认回复(文本信息) /// </summary> /// <param name="toUserName"></param> /// <param name="fromUserName"></param> /// <param name="createTime"></param> /// <param name="content"></param> /// <returns></returns> private string DefaultInfo(string toUserName, string fromUserName, int createTime, string content) { string msg = "<xml>" + "<ToUserName><![CDATA[" + toUserName + "]]></ToUserName>" + "<FromUserName><![CDATA[" + fromUserName + "]]></FromUserName>" + "<CreateTime>" + createTime + "</CreateTime>" + "<MsgType><![CDATA[text]]></MsgType>" + "<Content><![CDATA[" + content + "]]></Content>" + "<FuncFlag>0</FuncFlag>" + "</xml>"; return msg; } /// <summary> /// 转换为时间戳 /// </summary> /// <param name="time"></param> /// <returns></returns> private int ConvertDateTimeInt(System.DateTime time) { DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); return (int)(time - startTime).TotalSeconds; } /// <summary> /// 输出响应数据 /// </summary> /// <param name="obj"></param> private void ResponseWrite(object obj) { CancelCache(); Response.ContentType = "text/plain"; Response.Charset = "UTF-8"; Response.Write(obj != null ? obj : ""); Response.End(); } /// <summary> /// 取消缓存 /// </summary> private void CancelCache() { Response.BufferOutput = true; Response.Cache.SetExpires(DateTime.Now.AddMilliseconds(-1)); Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.AppendHeader("Pragma", "No-Cache"); }
试试其它关键字
同语言下
.
C#实现的html内容截取
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
.
实现对图片上传的接收
.
去除字符串中的空格,回车,换行符转变成‘;’在按‘
.
按照回车换行符分割字符串
.
文件MD5码 比较,检测文件是否一样
可能有用的
.
C#实现的html内容截取
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
.
实现对图片上传的接收
.
去除字符串中的空格,回车,换行符转变成‘;’在按‘
.
按照回车换行符分割字符串
.
文件MD5码 比较,检测文件是否一样
德帅
贡献的其它代码
(
7
)
.
模拟百度URL加密解密算法
.
逐个取一串字符串的字符
.
Java脚本获取EL表达式的值
.
利用cookie记忆当前位置的防刷新导航效果
.
微信公众平台开发C#版
.
让Chrome支持小于12px 的文字
.
通过POST方法发送数据
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3