找传奇、传世资源到传世资源站!

asp.net 微信公众号开发示例源码

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

主要实现了微信服务器与外站数据交换功能。from clipboard
using System;using System.Collections.Generic;using System.Web;using System.IO;using System.Text;using System.Web.Security;using System.Xml;using System.Reflection;using WeiXin.Models;namespace WeiXinService{ /// <summary> /// 微信数据处理 /// </summary> public class interfaceTest : IHttpHandler { log4net.ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);//自获取出错的类 public void ProcessRequest(HttpContext param_context) { try { string postString = string.Empty; //用户发送消息或点击等事件一般都是POST过来,微信服务器向接口发送POST请求,根据请求我们进行处理反馈 if (HttpContext.Current.Request.HttpMethod.ToUpper() == "POST") { using (Stream stream = HttpContext.Current.Request.InputStream) { Byte[] postBytes = new Byte[stream.Length]; stream.Read(postBytes, 0, (Int32)stream.Length); postString = Encoding.UTF8.GetString(postBytes); Handle(postString); } } else { //第一次配置接口地址的时候,微信服务器会向接口发送一个GET请求来验证你的接口地址 InterfaceTest(); } } catch (Exception ex) { log.Error(ex); } } /// <summary> /// 接收微信发送的XML消息并且解析 /// </summary> private void ReceiveXml() { Stream requestStream = System.Web.HttpContext.Current.Request.InputStream; byte[] requestByte = new byte[requestStream.Length]; requestStream.Read(requestByte, 0, (int)requestStream.Length); string requestStr = Encoding.UTF8.GetString(requestByte); if (!string.IsNullOrEmpty(requestStr)) { //封装请求类 XmlDocument requestDocXml = new XmlDocument(); requestDocXml.LoadXml(requestStr); XmlElement rootElement = requestDocXml.DocumentElement; WxXmlModel WxXmlModel = new WxXmlModel(); WxXmlModel.ToUserName = rootElement.SelectSingleNode("ToUserName").InnerText; WxXmlModel.FromUserName = rootElement.SelectSingleNode("FromUserName").InnerText; WxXmlModel.CreateTime = rootElement.SelectSingleNode("CreateTime").InnerText; WxXmlModel.MsgType = rootElement.SelectSingleNode("MsgType").InnerText; switch (WxXmlModel.MsgType) { case "text"://文本 WxXmlModel.Content = rootElement.SelectSingleNode("Content").InnerText; break; case "image"://图片 WxXmlModel.PicUrl = rootElement.SelectSingleNode("PicUrl").InnerText; break; case "event"://事件 WxXmlModel.Event = rootElement.SelectSingleNode("Event").InnerText; if (WxXmlModel.Event == "subscribe")//关注类型 { WxXmlModel.EventKey = rootElement.SelectSingleNode("EventKey").InnerText; } break; default: break; } // ResponseXML(WxXmlModel);//回复消息 } } /// <summary> /// 处理信息并应答 /// </summary> private void Handle(string postStr) { messageHelp help = new messageHelp(); string responseContent = help.ReturnMessage(postStr); log.Info("接收用户:" postStr "返回:" responseContent); HttpContext.Current.Response.ContentEncoding = Encoding.UTF8; HttpContext.Current.Response.Write(responseContent); } /// <summary> /// 成为开发者url测试,返回echoStr /// </summary> public void InterfaceTest() { string token = "token"; if (string.IsNullOrEmpty(token)) { return; } //微信服务器会将下面几个参数发送到接口,接口这边返回接收到的echoStr就说明验证通过, //主要为了防止别人盗用你的接口,我这边没做逻辑判断直接返回接收到的echoStr来通过验证 string echoString = HttpContext.Current.Request.QueryString["echoStr"]; string signature = HttpContext.Current.Request.QueryString["signature"]; string timestamp = HttpContext.Current.Request.QueryString["timestamp"]; string nonce = HttpContext.Current.Request.QueryString["nonce"]; log.Info("第一次配置接口: echoString:" echoString ",signature:" signature ",timestamp:" timestamp ",nonce:" nonce); if (!string.IsNullOrEmpty(echoString)) { HttpContext.Current.Response.Write(echoString); HttpContext.Current.Response.End(); } } public bool IsReusable { get { return false; } } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复