asp.net开发微信群微信众平台之获取用户消息并解决
这篇文章主要说明了asp.net开发微信群众平台之获取用户消息并解决的相干资料,需求的同伴可能考下
用户发送的消息是在微信服务器发送的一个HTTP POST申请中蕴含的,获取用户发送的消息要从POST申请的数据流中获取
微信服务器推送消息到效劳器的HTTP申请报文示例
POST /cgi-bin/wxpush? msg_signature=477715d11cdb4164915debcba66cb864d751f3e6×tamp=1409659813&nonce=1372623149 HTTP/1.1
Host: qy.weixin.qq.com
从POST申请中获取数据

这样获得的用户消息能够有两种情况:加密后的消息或是未加密的消息,微信群二维码,这与你在微信公共平台配置网站时 消息加解密形式的选取 无关,最好的微信群,大微信群,假设抉择了明文形式,则不会加密,假设抉择了兼容形式,则密文和明文都存在,最好的微信群,假设抉择的是平安形式,则用户消息会被加密,需求解密后能力进一步解决
2.回复用户消息
参考微信公共平台开发文档
•文本消息
?
| 1 2 3 4 5 6 7 | <xml> <ToUserName><![CDATA[{0}]]></ToUserName> <FromUserName><![CDATA[{1}]]></FromUserName> <CreateTime>{2}</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[{3}]]></Content> </xml> |
•图片消息
?
| 1 2 3 4 5 6 7 8 9 | <xml> <ToUserName><![CDATA[{0}]]></ToUserName> <FromUserName><![CDATA[{1}]]></FromUserName> <CreateTime>{2}</CreateTime> <MsgType><![CDATA[image]]></MsgType> <Image> <MediaId><![CDATA[{3}]]></MediaId> </Image> </xml> |
消息格局已经有了,接着咱们只有要设置相应的参数即可。
?
| 1 2 3 4 5 | responseContent = string.Format(ReplyType.Message_Text, FromUserName.InnerText, ToUserName.InnerText, DateTime.Now.Ticks, String.IsNullOrEmpty(reply)?"Sorry,I can not follow you." :reply); |
3.用户消息与效劳器消息的加密解密
微信公共平台开发者文档中提供有c++,C#,java等各种言语的加密解密示例,咱们用到的是C#,只有要将其中的两个文件减少到名目中即可,大微信群,Sample.cs是微信团队给出的示例代码,不需求引用,对
WXBizMsgCrypt.cs与Cryptography.cs文件减少引用即可。为了进一步封装和不便调用,微信群号,我又新建了一个类WeChatSecurityHelper
类中的定义两个方法,有微信群,区分来停止加密(EncryptMsg)和解密(DecryptMsg),创建一个WXBizMsgCrypt对象,调用它的方法加解密,新微信群,详细代码可见代码示例

WeChatSecurityHelper
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Common { public class WeChatSecurityHelper { /// <summary> /// 定义Token,与微信公共平台上的Token放弃分歧 /// </summary> private const string Token = "StupidMe"; /// <summary> /// AppId 要与 微信公共平台 上的 AppId 放弃分歧 /// </summary> private const string AppId = "11111111111"; /// <summary> /// 加密用 /// </summary> private const string AESKey = "pvX2KZWRLQSkUAbvArgLSAxCwTtxgFWF3XOnJ9iEUMG"; private static Tencent.WXBizMsgCrypt wxcpt = new Tencent.WXBizMsgCrypt(Token, AESKey, AppId); private string signature,timestamp,nonce; private static LogHelper logger = new LogHelper(typeof(WeChatSecurityHelper)); public WeChatSecurityHelper(string signature, string timestamp, string nonce) { this.signature = signature; this.timestamp = timestamp; this.nonce = nonce; } /// <summary> /// 加密消息 /// </summary> /// <param name="msg">要加密的消息</param> /// <returns>加密后的消息</returns> public string EncryptMsg(string msg) { string encryptMsg=""; int result = wxcpt.EncryptMsg(msg, timestamp, nonce, ref encryptMsg); if (result == 0) { return encryptMsg; } else { logger.Error("消息加密失败"); return ""; } } /// <summary> /// 解密消息 /// </summary> /// <param name="msg">消息体</param> /// <returns>明文消息</returns> public string DecryptMsg(string msg) { string decryptMsg = ""; int result = wxcpt.DecryptMsg(signature, timestamp, nonce, msg,ref decryptMsg); if (result != 0) { logger.Error("消息解密失败,result:"+result); } return decryptMsg; } } } |
以上所述就是本文的全副内容了,微信群号,最大的微信群,宿愿大家可以青睐。
本文来自网络采集,文中内容和观点不代表本站立场,本站不保证本文的准确性和真实性.关注微信群大全看更多微信群百科.



