博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.NET 微信开放平台接口
阅读量:5101 次
发布时间:2019-06-13

本文共 5621 字,大约阅读时间需要 18 分钟。

<%@ 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
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 = "
" + "
" + "
" + ConvertDateTimeInt(DateTime.Now) + "
" + "
0
"; 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; } }}

 

1.如果是为了验证微信接口的token是否通过, 将valid注释去掉

2.如果要返回给用户值, 注释掉valid

转载于:https://www.cnblogs.com/TivonStone/archive/2012/12/21/2827347.html

你可能感兴趣的文章
linux命令
查看>>
类库与框架,强类型与弱类型的闲聊
查看>>
webView添加头视图
查看>>
php match_model的简单使用
查看>>
在NT中直接访问物理内存
查看>>
Intel HEX 文件格式
查看>>
SIP服务器性能测试工具SIPp使用指导(转)
查看>>
php_扑克类
查看>>
回调没用,加上iframe提交表单
查看>>
(安卓)一般安卓开始界面 Loding 跳转 实例 ---亲测!
查看>>
Mysql 索引优化 - 1
查看>>
LeetCode(3) || Median of Two Sorted Arrays
查看>>
大话文本检测经典模型:EAST
查看>>
文本主题模型之LDA(一) LDA基础
查看>>
linux基础命令-chgrp/chown/chomd
查看>>
待整理
查看>>
iOS 6
查看>>
Nginx入门篇-基础知识与linux下安装操作
查看>>
一次动态sql查询订单数据的设计
查看>>
C# 类(10) 抽象类.
查看>>