标签:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Xml;
namespace ITCastWeiXin_demo
{
/// <summary>
/// weixin 的摘要说明
/// </summary>
public class weixin : IHttpHandler
{
//private static string msg;
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
if (context.Request.HttpMethod.ToLower().Equals("get"))
{
//context.Response.Write(msg);
//校验url
ValidateUrl();
}
else
{
//接收并响应
HandleMsg();
}
}
private void HandleMsg()
{
/*ToUserName 开发者微信号
FromUserName 发送方帐号(一个OpenID)
CreateTime 消息创建时间 (整型)
MsgType text
Content 文本消息内容
MsgId 消息id,64位整型*/
HttpContext context = HttpContext.Current;
//接收xml数据包
Stream xmlStream = context.Request.InputStream;
//构造xml对象
XmlDocument doc = new XmlDocument();
doc.Load(xmlStream);
XmlElement rootElement = doc.DocumentElement;//获取根节点
//接收xml
/*<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>1348831860</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[this is a test]]></Content>
<MsgId>1234567890123456</MsgId>
</xml>
*/
//解析xml数据
string toUserName = rootElement.SelectSingleNode("ToUserName").InnerText;
string fromUserName = rootElement.SelectSingleNode("FromUserName").InnerText;
string createTime = rootElement.SelectSingleNode("CreateTime").InnerText;
string msgType = rootElement.SelectSingleNode("MsgType").InnerText;
string content = rootElement.SelectSingleNode("Content").InnerText;
string msgId = rootElement.SelectSingleNode("MsgId").InnerText;
//msg = string.Format("{0}-{1}-{2}-{3}",toUserName,fromUserName,msgType,content);
//响应
/*<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[郑州网站建设]]></Content>
</xml>
*/
string xmlMsg = "<xml>" +
"<ToUserName><![CDATA["+fromUserName+"]]></ToUserName>" +
"<FromUserName><![CDATA["+toUserName+"]]></FromUserName>" +
"<CreateTime>12345678</CreateTime>" +
"<MsgType><![CDATA[text]]></MsgType>" +
"<Content><![CDATA[郑州网站建设]]></Content>" +
"</xml>";
context.Response.Write(xmlMsg);
sendMsgToManage(fromUserName, content);//把用户发送的消息发送给管理员
}
private int GetCreateTime()
{
DateTime startDate = new DateTime(1970,1,1,8,0,0);
return (int)(DateTime.Now - startDate).TotalSeconds;
}
private void ValidateUrl()
{
/*signature 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
timestamp 时间戳
nonce 随机数
echostr 随机字符串*/
//接收请求过来的参数
HttpContext context = HttpContext.Current;
string signature = context.Request["signature"];
string timestamp = context.Request["timestamp"];
string nonce = context.Request["nonce"];
string echostr = context.Request["echostr"];
string token = "weixin";
string[] str1 = { token, timestamp, nonce };
Array.Sort(str1);
string str2 = string.Join("", str1);
//加密
string str3 = FormsAuthentication.HashPasswordForStoringInConfigFile(str2, "SHA1");
if (str3.ToLower().Equals(signature))
{
context.Response.Write(echostr);
}
}
private void sendMsgToManage(string toUserName,string content)
{
string managerweixinid = "wuchangjian001";
string xmlMsg = "<xml>" +
"<ToUserName><![CDATA[" + managerweixinid + "]]></ToUserName>" +
"<FromUserName><![CDATA[" + toUserName + "]]></FromUserName>" +
"<CreateTime>12345678</CreateTime>" +
"<MsgType><![CDATA[text]]></MsgType>" +
"<Content><![CDATA["+content+"]]></Content>" +
"</xml>";
HttpContext context = HttpContext.Current;
context.Response.Write(xmlMsg);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
标签:
原文地址:http://www.cnblogs.com/qishun/p/5754995.html