In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the ".net development of Wechat public platform example tutorial", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn the ". Net Development Wechat Public platform example tutorial"!
The specific implementation methods are as follows:
1. Description:
The public platform information interface provides a new message processing method for developers. Only after you apply to become a developer can you use the development functions of the public platform. Here you need to fill in a URL and a Token, which also require you to have your own server (public network server) resources, in which the Token can be entered by the developer at will. URL is the link address of the interface configuration information. In this article, I use the method of creating a simple website, configure the relevant interface information in the background program of one of the pages, then publish it to the public network server, and finally access the link address of this page is the URL that should be filled in here.
2. Interface configuration process:
1. URL access-verify signature:
The copy code is as follows:
Const string Token = "aka"; / / define a local variable that cannot be modified. The variable defined here should be the same as the Token entered in the API configuration information.
Protected void Page_Load (object sender, EventArgs e)
{
String postStr = ""
Valid (); / / verify the signature. When the page says "you have successfully become a developer of the public platform and can use the development features of the public platform" after the entered information is submitted, then you need to comment out the verification method to make the subsequent message reply work normally.
If (Request.HttpMethod.ToLower () = = "post") / / when an ordinary Wechat user sends a message to a public account, the Wechat server will POST the message to the filled URL
{
PostStr = PostInput ()
If (string.IsNullOrEmpty (postStr) = = false)
{
/ / WriteLog (postStr,Server); / / include in the diary
ResponseMsg (postStr)
}
}
}
Private void Valid ()
{
String echoStr = Request.QueryString ["echoStr"] .ToString ()
If (CheckSignature ())
{
If (! string.IsNullOrEmpty (echoStr))
{
Response.Write (echoStr)
Response.End ()
}
}
}
The copy code is as follows:
/ / /
/ / verify Wechat signature
/ / /
/ / /
Private bool CheckSignature ()
{
String signature = Request.QueryString ["signature"] .ToString ()
String timestamp = Request.QueryString ["timestamp"] .ToString ()
String nonce = Request.QueryString ["nonce"] .ToString ()
String [] ArrTmp = {Token, timestamp, nonce}
Array.Sort (ArrTmp); / / dictionary sort
String tmpStr = string.Join ("", ArrTmp)
TmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile (tmpStr, "SHA1"); / / encrypt the string with sha1
TmpStr = tmpStr.ToLower (); / / A pair of letters in a string are lowercase converted, and non-alphabetic characters are not processed
/ / WriteLog (tmpStr, Server); / / include in the log
If (tmpStr = = signature) / / the encrypted string obtained by the developer can be compared with signature to indicate that the request originated from Wechat. The developer verifies the request by verifying signature. If you confirm that the GET request is from Wechat server and return the echostr parameter content as is, the connection will take effect, otherwise the connection will fail.
{
Return true
}
Else
Return false
}
/ / /
/ get the data returned by post
/ / /
/ / /
Private string PostInput ()
{
Stream s = System.Web.HttpContext.Current.Request.InputStream
Byte [] b = new byte [s.Length]
S.Read (b, 0, (int) s.Length)
Return Encoding.UTF8.GetString (b)
}
/ / /
/ / return the result of Wechat information
/ / /
/ / /
Private void ResponseMsg (string weixinXML)
{
Try
{
XmlDocument doc = new XmlDocument ()
Doc.LoadXml (weixinXML); / / read XML string
XmlElement rootElement = doc.DocumentElement
XmlNode MsgType = rootElement.SelectSingleNode ("MsgType"); / / get the message type in the string
String resxml = ""
If (MsgType.InnerText = = "text") / / if the message type is text message
{
Var model = new
{
ToUserName = rootElement.SelectSingleNode ("ToUserName") .InnerText
FromUserName = rootElement.SelectSingleNode ("FromUserName") .InnerText
CreateTime = rootElement.SelectSingleNode ("CreateTime") .InnerText
MsgType = MsgType.InnerText
Content = rootElement.SelectSingleNode ("Content") .InnerText
MsgId = rootElement.SelectSingleNode ("MsgId") .InnerText
}
Resxml + = "" + ConvertDateTimeInt (DateTime.Now) + ""
If (! string.IsNullOrEmpty (model.Content)) / / if a message is received
{
If (model.Content.Contains ("Hello") | | model.Content.Contains ("Hello") | | model.Content.Contains ("hi") | | model.Content.Contains ("hello")) / / Hello
{
Resxml + = "0"
}
}
Else// did not receive the message
{
Resxml + = "0"
}
Response.Write (resxml)
}
If (MsgType.InnerText = = "image") / / if the message type is a picture message
{
Var model = new
{
ToUserName = rootElement.SelectSingleNode ("ToUserName") .InnerText
FromUserName = rootElement.SelectSingleNode ("FromUserName") .InnerText
CreateTime = rootElement.SelectSingleNode ("CreateTime") .InnerText
MsgType = MsgType.InnerText
PicUrl = rootElement.SelectSingleNode ("PicUrl") .InnerText
MsgId = rootElement.SelectSingleNode ("MsgId") .InnerText
}
Resxml + = "" + ConvertDateTimeInt (DateTime.Now) + "10"
Response.Write (resxml)
}
Else// if it is the rest of the message type
{
Var model = new
{
ToUserName = rootElement.SelectSingleNode ("ToUserName") .InnerText
FromUserName = rootElement.SelectSingleNode ("FromUserName") .InnerText
CreateTime = rootElement.SelectSingleNode ("CreateTime") .InnerText
}
Resxml + = "" + ConvertDateTimeInt (DateTime.Now) + "0"
Response.Write (resxml)
}
}
Catch (Exception ex)
{
Throw ex
}
Response.End ()
}
/ / /
/ convert datetime to unixtime
/ / /
/ / /
/ / /
Private int ConvertDateTimeInt (System.DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime (new System.DateTime (1970, 1,1))
Return (int) (time-startTime). TotalSeconds
}
/ / /
/ / write a log (for tracking). You can include the content you want to print into a text file for testing.
/ / /
Public static void WriteLog (string strMemo, HttpServerUtility server)
{
String filename = server.MapPath ("/ logs/log.txt"); / / create a folder named logs in the website project (then create a random web page file in the folder to prevent the website from not seeing the scheduled file after posting to the server)
If (! Directory.Exists (server.MapPath ("/ / logs//")
Directory.CreateDirectory ("/ / logs//")
StreamWriter sr = null
Try
{
If (! File.Exists (filename))
{
Sr = File.CreateText (filename)
}
Else
{
Sr = File.AppendText (filename)
}
Sr.WriteLine (strMemo)
}
Catch
{
}
Finally
{
If (sr! = null)
Sr.Close ()
}
}
At this point, I believe you have a deeper understanding of the ".net development of Wechat public platform example tutorial", you might as well to practice it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 253
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.