asp.net to develop micro-credit public platform to get user messages and handle _ practical tips

Source: Internet
Author: User
Tags cdata decrypt documentation http post

Get user Message

The message sent by the user is contained in an HTTP POST request sent by the micro-server to obtain the message sent by the user from the data stream of the POST request

Example of an HTTP request message that a micro-server pushes messages to a server

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

Host:qy.weixin.qq.com

Fetching data from a POST request

There are two possible scenarios for obtaining a user message: Encrypted message or unencrypted message, which is related to the selection of the message plus decryption mode when you configure the Web site on the micro-trust platform, if clear text mode is selected, it will not be encrypted, and if compatibility mode is selected, both ciphertext and plaintext exist, if Safe mode is selected, The user message is encrypted and needs to be decrypted before it can be processed further.

2. Reply User Message

Reference micro-credit common platform development Documentation

• Text messages

<xml> 
<tousername><![ Cdata[{0}]]></tousername> 
<fromusername><![ cdata[{1}]]></fromusername> 
<CreateTime>{2}</CreateTime> 
<msgtype><![ Cdata[text]]></msgtype> 
<content><![ Cdata[{3}]]></content> 
</xml>


• Picture Message

<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, and then we just need to set the appropriate parameters.

Responsecontent = string. Format (Replytype.message_text, 
 fromusername.innertext, 
 tousername.innertext, 
DateTime.Now.Ticks, 
String.IsNullOrEmpty (Reply)? " Sorry,i can not follow for you. ": Reply);

3. Encryption and decryption of user messages and server messages

Micro-Credit public Platform developer documentation in the C++,c#,java and other languages such as encryption and decryption examples, we use C #, only two of them to add to the project can be, Sample.cs is the micro-letter team to give the sample code, do not need to reference, the

WXBizMsgCrypt.cs with the Cryptography.cs file to add a reference. To further encapsulate and facilitate the call, I have created a new class Wechatsecurityhelper

Class to define two methods, respectively, to encrypt (encryptmsg) and Decrypt (decryptmsg), create a Wxbizmsgcrypt object, invoke its method and decrypt, the specific code can be seen code sample

Wechatsecurityhelper

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;

Using System.Threading.Tasks;
    Namespace Common {public class Wechatsecurityhelper {///<summary>///definition token, consistent with token on the micro-trust platform
    </summary> Private Const string Token = "Stupidme"; <summary>///AppId to be consistent with AppId on the micro-credit public platform///</summary> Private Const string AppId = "11111
    111111 "; <summary>///encryption with///</summary> Private Const string Aeskey = "Pvx2kzwrlqskuabvarglsaxcwttxg

    FWF3XONJ9IEUMG ";
    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>///Encrypted message///</summary>///<param name= "MSG" > Message </param> to encrypt
      <returns> Encrypted message </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 ("Message encryption failed");
      Return "";
    }///<summary>///decryption message///</summary>///<param name= "MSG" > Message body </param>
      <returns> PlainText message </returns> public string decryptmsg (String msg) {string decryptmsg = ""; int result = Wxcpt.
      Decryptmsg (signature, timestamp, nonce, Msg,ref decryptmsg); If (Result!= 0) {logger.
      Error ("Message decryption failed, result:" +result);
    return decryptmsg;
 }
  }
}

The above is the entire contents of this article, I hope you can enjoy.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.