Home  >  Article  >  WeChat Applet  >  Using asp.net to develop WeChat public platform to obtain user messages and process them

Using asp.net to develop WeChat public platform to obtain user messages and process them

高洛峰
高洛峰Original
2017-03-15 16:59:411861browse

This article mainly introduces the use of asp.net For relevant information on how to develop WeChat public platform to obtain user messages and process them, friends in need can refer to

Getting user messages

The messages sent by users are on the WeChat server Contained in an HTTP POST request sent, obtaining the message sent by the user must be obtained from the data stream of the POST request

Example of HTTP request message for the WeChat server to push the message to the server

POST /cgi-bin/wxpush? msg_signature=477715d11cdb4164915debcba66cb864d751f3e6&timestamp=1409659813&nonce=1372623149 HTTP/1.1

Host: qy.weixin.qq .com

Get data from POST request

Using asp.net to develop WeChat public platform to obtain user messages and process them

#The user message obtained in this way may be in two situations: encrypted message or unencrypted message. This is related to the message encryption and decryption mode you selected when configuring the website on the WeChat public platform. If you select plaintext mode, it will not be encrypted. If you select compatibility mode, both ciphertext and plaintext will exist. If you select Safe mode, user messages will be encrypted and need to be decrypted before further processing

2. Reply to user messages

Refer to WeChat public platform development documentation

•Text message

<xml> 
<ToUserName><![CDATA[{0}]]></ToUserName> 
<FromUserName><![CDATA[{1}]]></FromUserName> 
<CreateTime>{2}</CreateTime> 
<MsgType><![CDATA[text]]></MsgType> 
<Content><![CDATA[{3}]]></Content> 
</xml>


##•

PictureMessage

<xml> 
<ToUserName><![CDATA[{0}]]></ToUserName> 
<FromUserName><![CDATA[{1}]]></FromUserName> 
<CreateTime>{2}</CreateTime> 
<MsgType><![CDATA[image]]></MsgType> 
<Image> 
<MediaId><![CDATA[{3}]]></MediaId> 
</Image> 
</xml>

The message format is already there, then we just need to set the corresponding parameters

responseContent = string.Format(ReplyType.Message_Text, 
 FromUserName.InnerText, 
 ToUserName.InnerText, 
DateTime.Now.Ticks, 
String.IsNullOrEmpty(reply)?"Sorry,I can not follow you." :reply);

3. Encryption and decryption of user messages and server messages

The WeChat public platform developer documentation provides encryption and decryption examples in various languages ​​such as c++,

C#,java, etc. We are using C#. We only need to add two of the files to the project. Sample.cs is the sample code given by the WeChat team. There is no need to reference . For ## Just add references to the #WXBizMsg

Crypt

.cs and Cryptography.cs files. In order to further encapsulate and facilitate calling, I created a new class WeChatSecurityHelper and defined two methods respectively. To encrypt (EncryptMsg) and decrypt (DecryptMsg), create a WXBizMsgCrypt

object

, call its method to encrypt and decrypt, the specific code can be seen in the code example

Using asp.net to develop WeChat public platform to obtain user messages and process them WeChatSecurityHelper


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;
    }
  }
}

The above is the entire content of this article, I hope you all like it.

The above is the detailed content of Using asp.net to develop WeChat public platform to obtain user messages and process them. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn