Objective
When all preparations are in place, implement a focus on the public number and push a message to the client. Follow-up push messages require a GET request, a POST request, and a GET request is primarily to authenticate to the server, where the POST request is primarily processing messages. AppID and Appsecret passed when the interface is tuned, pass the parameters corresponding to the public number.
Event interaction
Event interaction is primarily the push of XML packets to the server
Look at the code
[HttpGet] [ActionName ("Index")]public actionresult Get (string signature,string timestamp,string nonce,string echostr) { if ( Checksignature.check (signature, timestamp, nonce, token)) { return Content (ECHOSTR); } else { return Content ("err"); }
[HttpPost] [ActionName ("Index")]public actionresult Get (string signature, string timestamp, string nonce) {StreamReader sr = new St Reamreader (Request.inputstream, Encoding.UTF8); XmlDocument doc = new XmlDocument (); Doc. Load (SR); Sr. Close (); Sr. Dispose (); Wxmessage wxmessage = new Wxmessage (); Wxmessage.tousername = doc. selectSingleNode ("xml"). selectSingleNode ("Tousername"). InnerText; Wxmessage.fromusername = doc. selectSingleNode ("xml"). selectSingleNode ("Fromusername"). InnerText; Wxmessage.msgtype = doc. selectSingleNode ("xml"). selectSingleNode ("Msgtype"). InnerText; wxmessage.createtime = Int. Parse (Doc. selectSingleNode ("xml"). selectSingleNode ("Createtime"). InnerText); if (Wxmessage.msgtype = = "Event") {Wxmessage.eventname = doc. selectSingleNode ("xml"). selectSingleNode ("Event"). InnerText; if (!string. IsNullOrEmpty (wxmessage.eventname) && wxmessage.eventname = = "Subscribe") {string content = "Hello, welcome to visit Gar Fieldzf8 test public platform "; Content = Sendtextmessage (wxmessage, content); return content (content); }} return Content ("");} private String Sendtextmessage (wxmessage wxmessage,string content) {string result = string. Format (Message, Wxmessage. Fromusername,wxmessage. Tousername,datetime.now.ticks, content); return result;} public string Message {get {return @ "<xml> <tousername><![ Cdata[{0}]]></tousername> <fromusername><! [cdata[{1}]]></fromusername> <CreateTime>{2}</CreateTime> <msgtype><! [cdata[text]]></msgtype> <content><! [cdata[{3}]]></content> </xml> "; } }
public class Wxmessage {public string Tousername {get; set;} public string Fromusername {get; set;} Public long Createtime {get; set;} public string Content {get; set;} public string Msgtype {get; set;} public string EventName {get; set;} public string Eventkey {get; set;}}
Summarize
The process of developing the interface cannot be debugged, the only way to troubleshoot the problem is to log in the key place.
The event interaction is primarily parsing the sent XML packet, parsing the XML, and sending it to response in the form of a message-specific format stitching XML. The checksignature used in the Get method is a class of the Shengpai SDK, that is, the signature check.
Primary Tousername and Fromusername when sending messages to clients. I started writing two arguments in reverse, causing the client to receive no messages.
The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.