Don’t want to listen to my bullshit, directly look at the official: wechat official documents: mp.weixin.qq.com/wiki?t=reso…
Note before development: we need to use StreamReader
Using system.io;
If we find that our users send link information to the public account. We’re dealing with the link information.
First let’s take a look at the official documentation.
<xml><ToUserName>< ![CDATA[toUser] ]></ToUserName><FromUserName>< ![CDATA[fromUser] ]></FromUserName><CreateTime>1351776360</CreateTime><MsgType>< ![CDATA[link] ]></MsgType><Title><! [CDATA[link to official website of public platform]]></Title><Description><! [CDATA[link to official website of public platform]]></Description><Url>< ![CDATA[url] ]></Url><MsgId>1234567890123456</MsgId></xml>
Copy the code
parameter | describe |
---|---|
ToUserName | Receiver’s wechat signal |
FromUserName | If the sender is an ordinary user, it is an OpenID |
CreateTime | Message creation time |
MsgType | Message type, link |
Title | News headlines |
Description | Message description |
Url | News link |
MsgId | The message ID is a 64-bit integer |
Now let’s talk about development
Here are some of the rules wechat gave us
We are now going to use these rules to get what we want.
Step 1: Get that string of XML. This is the most important, because without this XML, we have nothing. (Our source of information is this XML.)
Here I use StreamReader to get Request.Body
Because XML is mostly in Request.Body
// Declare an XML convenience call
string xml="";
// Get our stream
var reader = new StreamReader(Request.Body);
// Read the XML from the stream.
var contentFromBody = reader.ReadToEnd();
// Save it into our string for later use
xml = contentFromBody;
Copy the code
We read the information in the XML and turn it into our wechat message object.
Here? I borrowed the code from a big guy:
Several classes that specialize in handling wechat messages
The first class: MessageFromWeiXin:
The second class:
ResponseClient:
I’m not going to show these two classes. You can go to a few articles to see, if you don’t want to turn over the article, please leave your email, or directly contact me
With these two classes, we have a lot of work to do. If you find any errors, it’s because of the string extension class. Leave the mailbox if you need it. This thing is too long. It is not good to send, but we should all understand, their own write a set.
Use the method in the second class above to convert: XmlToMessageFromWeiXin
We get our MSG object
We have msgType in this object, and the type enumeration is for each event.
We’ll write a switch to separate them. Different events are processed in different branches.
/// <summary>
///Receive messages from wechat
/// </summary>
[HttpPost]
[Route("api/WeChat")]
public string Post(string xml)
{
System.Console.WriteLine("Enter message processing");
var reader = new StreamReader(Request.Body);
var contentFromBody = reader.ReadToEnd();
System.Console.WriteLine($"xml:{contentFromBody}");
xml = contentFromBody;
try
{
var msg = ResponseClient.XmlToMessageFromWeiXin(xml);
var Response = "";
switch (msg.MsgType)
{
case MessageFromWeiXinType.link:
var link = (LinkMessageFromWeiXin)msg;
// Our link is mainly the message Title; Message Description; Message link Url
System.Console.WriteLine($" Message title:{link.Title}\n Message description:{link.Description}\n Message link:{link.Url}\n");
Response = "
+ link.FromUserName +
"]]>
+ link.ToUserName +
"]]></FromUserName> <CreateTime>" +
ResponseClient.ConvertDateTimeToInt(DateTime.Now) +
"
, address:"+link.Url+"[]></Content> </ XML >"; break; } return Response; // return "success"; } catch (Exception ex) { return "error"; }}Copy the code
results
If you don’t understand or need code, please leave your email address
Welcome to qq group communication: 704028989