In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you ".net how to develop Wechat public platform", the content is easy to understand, clear, hope to help you solve your doubts, now let the editor lead you to study and learn ".net how to develop Wechat public platform" this article.
The http://www.php.cn/code/11829.html" target= "_ blank" > API 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. These two messages also require that you have your own server (public network server) resources, and the Token in which 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, and then publish it to the public network server. Finally, the link address of this page is the URL that should be filled in here.
Interface configuration process:
1. URL access-verify signature:
Const string Token = "aka"; / / define a local variable that cannot be modified. The variable defined here should be consistent with 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 so that the subsequent message reply can work properly if (Request.HttpMethod.ToLower () = = "post") / / when ordinary Wechat users send messages to the 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 ();}
/ 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"); / / A sha1 encryption of the string tmpStr = tmpStr.ToLower (); / / A pair of letters in the string are lowercase converted, and non-alphabetic characters are not processed / / WriteLog (tmpStr, Server) / / include 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 fails {return true;} else return false } / 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 Wechat information result / private void ResponseMsg (string weixinXML) {try {XmlDocument doc = new XmlDocument (); doc.LoadXml (weixinXML); / / read the XML string XmlElement rootElement = doc.DocumentElement; XmlNode MsgType = rootElement.SelectSingleNode ("MsgType"); / / get the message type string resxml = "" in the string 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 you receive a message {if (model.Content.Contains) | | model.Content.Contains (Hello) | | model.Content.Contains ("hi") | | model.Content.Contains ("hello") / / Hello {resxml + = "0" }} else// did not receive message {resxml + = "0";} Response.Write (resxml) } if (MsgType.InnerText = = "image") / / if the message type is 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) + "1"; Response.Write (resxml) } else// if 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 () } / datetime is converted 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 log (for tracking). You can count the content you want to print into a text file to test / 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 randomly create a web page file in the folder to avoid the website not seeing the scheduled file after publishing 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 ();}} above is all the content of this article entitled ".net how to develop Wechat Public platform". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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: 0
*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.