做者:王先荣git
最近在学习微信公众号开发,将学习的成果作成了一个类库,方便重复使用。服务器
如今微信公众号多如牛毛,开发微信的高手能够直接无视这个系列的文章了。微信
使用该类库的流程及寥寥数行代码获得的结果以下。ide
本文的源代码主要在:http://git.oschina.net/xrwang2/xrwang.weixin.PublicAccount/blob/master/xrwang.net/WeixinInterface.ashx以及http://git.oschina.net/xrwang2/xrwang.weixin.PublicAccount/blob/master/xrwang.net/Global.asaxpost
1 引用微信公众号类库性能
引用xrwang.weixin.PublicAccount。学习
2 添加公众号信息测试
添加提供服务的公众号的信息,包括:原始id、AppId、AppSecret、EncodingAESKey。代码以下:优化
AccountInfoCollection.SetAccountInfo(new AccountInfo("YourOriginalId", "AppId", "AppSecret", "Token", "EncodingAESKey"));
若是须要同时给多个公众号提供服务,重复上面这行代码就能够了。spa
注:因为微信服务器的缘由,若是在一个站点中要同时给多个公众号提供服务,有两种方法:若是用同一页面处理多个公众号,那么Token必须一致(1)能够在接口配置信息的URL中加入区分公众号的参数(例如:http://www.xrwang.net/WeixinInterface.ashx?username=gh_5dbae931ec49);(2)针对每一个公众号单独创建一个页面来处理。我目前采用了第一种方式,这样更简便。
我喜欢将添加公众号信息的工做放到Gobal.asax的Application_Start方法中。
3 与微信服务器通讯
我添加了名为“WeixinInterface.ashx”的通常处理页,并在其中与微信服务器进行通讯,包括:校验请求、处理请求、回复适当的响应。代码以下:
public void ProcessRequest(HttpContext context) { string result = string.Empty; if (Validate(context)) { if (context.Request.HttpMethod == WebRequestMethods.Http.Get) result = HandleGet(context); else if (context.Request.HttpMethod == WebRequestMethods.Http.Post) result = HandlePost(context); } else Message.Insert(new Message(MessageType.Exception, "校验消息失败。\r\n地址:" + context.Request.RawUrl)); context.Response.Write(result); }
3.1 校验请求
首先,咱们须要校验接收到的请求是否来自微信服务器,方法以下:
/// <summary> /// 验证消息的有效性 /// </summary> /// <param name="context"></param> /// <returns>若是消息有效,返回true;不然返回false。</returns> private bool Validate(HttpContext context) { string token = AccountInfoCollection.First.Token; //因为在校验微信签名时,微信未传入公众号,所以这里用第一个公众号的TOKEN string signature = RequestEx.TryGetQueryString("signature"); string timestamp = RequestEx.TryGetQueryString("timestamp"); string nonce = RequestEx.TryGetQueryString("nonce"); if (string.IsNullOrWhiteSpace(signature) || string.IsNullOrWhiteSpace(timestamp) || string.IsNullOrWhiteSpace(nonce)) return false; return xrwang.weixin.PublicAccount.Utility.CheckSignature(signature, token, timestamp, nonce); }
固然,若是你对世界充满爱,相信没有欺骗;若是你厉行节约,急需提升性能;不校验也是能够的。
3.2 处理请求
校验完请求以后,咱们分两种状况处理请求:
(1)微信服务器的GET请求,用来验证咱们的服务器是否正在工做,咱们直接返回echostr就能够了;
/// <summary> /// 处理微信的GET请求,校验签名 /// </summary> /// <param name="context"></param> /// <returns>返回echostr</returns> private string HandleGet(HttpContext context) { return RequestEx.TryGetQueryString("echostr"); }
(2)微信服务器的POST请求,这是服务器分发给咱们的消息,咱们须要解析消息。
RequestMessageHelper helper = new RequestMessageHelper(context.Request);
3.3 回复响应
解析完微信服务器分发给咱们的消息以后,咱们要作出回应。我这里把收到的消息直接发回去,偷懒~\(≧▽≦)/~啦啦啦
/// <summary> /// 处理微信的POST请求 /// </summary> /// <param name="context"></param> /// <returns>返回xml响应</returns> private string HandlePost(HttpContext context) { RequestMessageHelper helper = new RequestMessageHelper(context.Request); if (helper.Message != null) { ResponseBaseMessage responseMessage = HandleRequestMessage(helper.Message); return responseMessage.ToXml(helper.EncryptType); } else return string.Empty; } /// <summary> /// 处理请求消息,返回响应消息 /// </summary> /// <returns>返回响应消息</returns> private ResponseBaseMessage HandleRequestMessage(RequestBaseMessage requestMessage) { ResponseTextMessage response = new ResponseTextMessage(requestMessage.FromUserName, requestMessage.ToUserName, DateTime.Now, string.Format("自动回复,请求内容以下:\r\n{0}", requestMessage)); response.Content += "\r\n<a href=\"http://www.cnblogs.com\">博客园</a>"; return response; }
固然了,正常状况下,咱们须要兵来将挡水来土掩,根据不一样的请求,回复对应的响应。若是须要对请求排队,再一一回复客服消息,能够先直接回复空字符串。回复客服消息的方法请看后面的文章。
4 微信公众号类库简介
xrwang.weixin.PublicAccount是一套简化微信公众号开发的类库,由王先荣开发,而且正在添砖加瓦中。采用MIT开源协议,你们能够随便用,别删掉个人名字就能够啦。
源代码的地址是:http://git.oschina.net/xrwang2/xrwang.weixin.PublicAccount
若是发现BUG,请在博客中留言,或者给我发电子邮件:xrwang(a)126.com。
千万不要用QQ或者阿里旺旺聊天,打扰我玩游戏,我会骂人的 >.<
5 体验测试号
下面分别是个人测试号和公众号,您能够对照文章来体验哦。
测试号 |
|
测试号权限多,几乎能够测试公众平台的全部功能。 |
个人公众号 xrwang |
|
我的订阅号,功能较少,不过我会特别优化。 |
好了,感谢您看完本文,但愿对您有所帮助。本文来自xrwang的博客http://xrwang/cnblogs.com,欢迎在不篡改做者的前提下转载以传播知识。