Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to develop Wechat with C #

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to do Wechat development C#, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

1. Must have a Wechat public account

two。 You can also apply for a test WeChat account and link to your http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login.

Then, set up mvc, and you only need to click vs to help you out. This is nonsense.

Next, to create a general handler, randomly choose a name, test pass is the key, seize the time …...

/ verify Wechat signature / * sort the three parameters token, timestamp and nonce in lexicographic order / * concatenate the three parameter strings into a string for sha1 encryption / * the encrypted string obtained by the developer can be compared with signature to indicate that the request originated from Wechat. Private bool CheckSignature () {var token = "token"; var signature = HttpContext.Current.Request.QueryString ["signature"]; var timestamp = HttpContext.Current.Request.QueryString ["timestamp"]; var nonce = HttpContext.Current.Request.QueryString ["nonce"]; var echostr = HttpContext.Current.Request.QueryString ["echostr"]; string [] ArrTmp = {token, timestamp, nonce}; Array.Sort (ArrTmp); / / in dictionary order var tmpStr = string.Join ("", ArrTmp) " TmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile (tmpStr, "SHA1"); / / encryption method if (tmpStr.ToLower () = = signature) {return true;} return false;}

This code is equivalent to shaking hands with an one-to-one token communication of Token you wrote in the official account of Wechat. As long as you communicate with each other, then you are done!

Get it done later, there is still work to be done, keep writing!

How to configure, this is a problem, ah, can only use peanut shells to test first, at least to know if playing for a long time is not a pass!

See the following figure: peanut shell configuration on the left-iis website release binding on the right

Seeing this picture, you can see how to play next. The local iis is equipped with a domain name. This is really cool.

Down there. We add code. Set up send and auto reply tests to see if you can play

# region receives messages / receives XML messages sent by Wechat and parses / private void ReceiveXml () {var requestStream = HttpContext.Current.Request.InputStream; var requestByte = new byte [requestStream.Length]; requestStream.Read (requestByte, 0, (int) requestStream.Length); var requestStr = Encoding.UTF8.GetString (requestByte); if (! string.IsNullOrEmpty (requestStr)) {/ / Encapsulation request class var requestDocXml = new XmlDocument (); requestDocXml.LoadXml (requestStr) Var rootElement = requestDocXml.DocumentElement; if (rootElement = = null) return; var wxXmlModel = new WxXmlModel {ToUserName = rootElement.SelectSingleNode ("ToUserName"). InnerText, FromUserName = rootElement.SelectSingleNode ("FromUserName"). InnerText, CreateTime = rootElement.SelectSingleNode ("CreateTime"). InnerText, MsgType = rootElement.SelectSingleNode ("MsgType"). InnerText}; switch (wxXmlModel.MsgType) {case "text": / / text wxXmlModel.Content = rootElement.SelectSingleNode ("Content"). InnerText; break Case "image": / / Picture wxXmlModel.PicUrl = rootElement.SelectSingleNode ("PicUrl"). InnerText; break; case "event": / / event wxXmlModel.Event = rootElement.SelectSingleNode ("Event"). InnerText; if (wxXmlModel.Event! = "TEMPLATESENDJOBFINISH") / / interest type {wxXmlModel.EventKey = rootElement.SelectSingleNode ("EventKey"). InnerText;} break; default: break;} ResponseXML (wxXmlModel) / / reply message} # endregion # region reply message private void ResponseXML (WxXmlModel WxXmlModel) {var QrCodeApi = new QrCodeApi (); var XML = ""; switch (WxXmlModel.MsgType) {case "text": / / text reply XML = ResponseMessage.GetText (WxXmlModel.FromUserName, WxXmlModel.ToUserName, WxXmlModel.Content); break Case "event": switch (WxXmlModel.Event) {case "subscribe": if (string.IsNullOrEmpty (WxXmlModel.EventKey)) {XML = ResponseMessage.GetText (WxXmlModel.FromUserName, WxXmlModel.ToUserName, "follow success");} else {XML = ResponseMessage.SubScanQrcode (WxXmlModel.FromUserName, WxXmlModel.ToUserName, WxXmlModel.EventKey); / / scan QR code follow first and then push event} break Case "SCAN": XML = ResponseMessage.ScanQrcode (WxXmlModel.FromUserName, WxXmlModel.ToUserName, WxXmlModel.EventKey); / / scan the QR code with parameters to follow the direct push event break;} break; default:// default reply break;} HttpContext.Current.Response.Write (XML); HttpContext.Current.Response.End ();} # endregion

The above one is sent, the other is received, or in the WhApi.ashx processor file. I just want to be clear, hehe!

Because your handshake on the public platform is successful, you must send something to try, right?

The following picture is an association between a receiving method and an automatic matching reply file, don't worry, I will upload this file below!

Lack of a configuration, that is, vs to set [debug]-[attach to the process], just put a tick below [Show all user processes], you can find w3wp.exe if there are multiple such processes, then you still have to confirm the [user name] column, select the same as your program pool name on the ok, click attach, make sure to attach!

Next. It's fun...

Wechat scan the test official account, send a custom message to see if there is any reply, the tedious configuration above, you can add breakpoints to debug, otherwise there is no point to do so, see that sending and receiving match with your own settings, then ok.

The above is all the content of this article "how to develop Wechat with C #". 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report