代码语言
.
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
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
.NET 微信开放平台接口
作者:
石世特
/ 发布于
2013/4/22
/
1856
1.如果是为了验证微信接口的token是否通过, 将valid注释去掉 2.如果要返回给用户值, 注释掉valid
<%@ WebHandler Language="C#" Class="WeixinInterface" %> using System; using System.Web; public class WeixinInterface : IHttpHandler { HttpContext context = null; string postStr = ""; public void ProcessRequest(HttpContext param_context) { context = param_context; //以写日志为荣,以打断点为耻. //WriteLog("before valid \n"); //valid();//用于验证 //WriteLog("after valid, before post \n"); if (context.Request.HttpMethod.ToLower() == "post") { System.IO.Stream s = context.Request.InputStream; byte[] b = new byte[s.Length]; s.Read(b, 0, (int)s.Length); postStr = System.Text.Encoding.UTF8.GetString(b); if (!string.IsNullOrEmpty(postStr)) { responseMsg(postStr); } //WriteLog("-------AfterResponseMsg:-------\n" + postStr); } } public void valid() { var echostr = context.Request["echoStr"].ToString(); if (checkSignature() && !string.IsNullOrEmpty(echostr)) { context.Response.Write(echostr); context.Response.End();//推送...不然微信平台无法验证token } } public bool checkSignature() { var signature = context.Request["signature"].ToString(); var timestamp = context.Request["timestamp"].ToString(); var nonce = context.Request["nonce"].ToString(); var token = "faketoken"; string[] ArrTmp = { token, timestamp, nonce }; Array.Sort(ArrTmp); //字典排序 string tmpStr = string.Join("", ArrTmp); tmpStr = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1"); tmpStr = tmpStr.ToLower(); if (tmpStr == signature) { return true; } else { return false; } } public string GetSha1(System.Collections.Generic.List<string> codelist) { codelist.Sort(); var combostr = string.Empty; for (int i = 0; i < codelist.Count; i++) { combostr += codelist[i]; } return EncryptToSHA1(combostr); } public string EncryptToSHA1(string str) { System.Security.Cryptography.SHA1CryptoServiceProvider sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider(); byte[] str1 = System.Text.Encoding.UTF8.GetBytes(str); byte[] str2 = sha1.ComputeHash(str1); sha1.Clear(); (sha1 as IDisposable).Dispose(); return Convert.ToBase64String(str2); } public void responseMsg(string postStr) { System.Xml.XmlDocument postObj = new System.Xml.XmlDocument(); postObj.LoadXml(postStr); WriteLog("responseMsg:-------" + postStr); var FromUserNameList = postObj.GetElementsByTagName("FromUserName"); string FromUserName = string.Empty; for (int i = 0; i < FromUserNameList.Count; i++) { if (FromUserNameList[i].ChildNodes[0].NodeType == System.Xml.XmlNodeType.CDATA) { FromUserName = FromUserNameList[i].ChildNodes[0].Value; } } var toUsernameList = postObj.GetElementsByTagName("ToUserName"); string ToUserName = string.Empty; for (int i = 0; i < toUsernameList.Count; i++) { if (toUsernameList[i].ChildNodes[0].NodeType == System.Xml.XmlNodeType.CDATA) { ToUserName = toUsernameList[i].ChildNodes[0].Value; } } var keywordList = postObj.GetElementsByTagName("Content"); string Content = string.Empty; for (int i = 0; i < keywordList.Count; i++) { if (keywordList[i].ChildNodes[0].NodeType == System.Xml.XmlNodeType.CDATA) { Content = keywordList[i].ChildNodes[0].Value; } } var time = DateTime.Now; var textpl = "<xml><ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>" + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>" + "<CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[text]]></MsgType>" + "<Content><![CDATA[欢迎来到微信世界---" + Content + "]]></Content><FuncFlag>0</FuncFlag></xml> "; context.Response.Write(textpl); context.Response.End(); } private DateTime UnixTimeToTime(string timeStamp) { DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); long lTime = long.Parse(timeStamp + "0000000"); TimeSpan toNow = new TimeSpan(lTime); return dtStart.Add(toNow); } private int ConvertDateTimeInt(System.DateTime time) { System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); return (int)(time - startTime).TotalSeconds; } private void WriteLog(string strMemo) { string filename = "D:/WEBHOME/logs/log.txt"; if (!System.IO.Directory.Exists("D:/WEBHOME/logs/")) System.IO.Directory.CreateDirectory("D:/WEBHOME/logs/"); System.IO.StreamWriter sr = null; try { if (!System.IO.File.Exists(filename)) { sr = System.IO.File.CreateText(filename); } else { sr = System.IO.File.AppendText(filename); } sr.WriteLine(strMemo); } catch { } finally { if (sr != null) sr.Close(); } } public bool IsReusable { get { return false; } } }
试试其它关键字
.NET
微信
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
石世特
贡献的其它代码
(
2
)
.
仿微信二维码生成二维码中间显示头像
.
.NET 微信开放平台接口
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3