Introduction to using C# WeChat development instructions
Mar 15, 2017 pm 05:05 PMThis article is mainly for everyone to use the detailed introduction of C#WeChat development instructions. It is of great reference value and practicality. Interested friends can refer to it
If you don’t want to talk nonsense, just go directly wrote! Because it is left for you to write essays, so if you see it, please don’t complain...
1. You must have a WeChat public account
2. You can also apply for a test WeChat account, the link is given to youhttp://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
Then, to create mvc, you only need to click a few Download VS and it will be done for you. This is no nonsense
Next, you need to create a general handler, give it a name casually, passing the test is the key, hurry up...
/// <summary> /// 驗(yàn)證微信簽名 /// </summary> /// <returns></returns> /// * 將token、timestamp、nonce三個(gè)參數(shù)進(jìn)行字典序排序 /// * 將三個(gè)參數(shù)字符串拼接成一個(gè)字符串進(jìn)行sha1加密 /// * 開發(fā)者獲得加密后的字符串可與signature對(duì)比,標(biāo)識(shí)該請(qǐng)求來(lái)源于微信。 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); //字典排序 var tmpStr = string.Join("", ArrTmp); tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");//加密方式 if (tmpStr.ToLower() == signature) { return true; } return false; }
This The code is equivalent to a one-to-one token communication handshake with the Token you wrote in [Development] - [Basic Configuration] of the WeChat official account. As long as they communicate with each other, then you are done!
Finishing it is a matter for later, there is still work to be done, let’s continue writing!
How to configure it? This is the problem. You can only use peanut shells to test it first. At least you need to know whether it works after playing for a long time!
Look at the picture below: Peanut shell configuration on the left-----iis website publishing binding on the right
See this picture, you also understand the next step How to play. The local iis is equipped with a domain name. This is so fucking awesome...
Below. We add code. Set up sending and automatic reply tests to see if you can play
#region 接收消息 /// <summary> /// 接收微信發(fā)送的XML消息并且解析 /// </summary> 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)) { //封裝請(qǐng)求類 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"://文本 wxXmlModel.Content = rootElement.SelectSingleNode("Content").InnerText; break; case "image"://圖片 wxXmlModel.PicUrl = rootElement.SelectSingleNode("PicUrl").InnerText; break; case "event"://事件 wxXmlModel.Event = rootElement.SelectSingleNode("Event").InnerText; if (wxXmlModel.Event != "TEMPLATESENDJOBFINISH")//關(guān)注類型 { wxXmlModel.EventKey = rootElement.SelectSingleNode("EventKey").InnerText; } break; default: break; } ResponseXML(wxXmlModel);//回復(fù)消息 } } #endregion #region 回復(fù)消息 private void ResponseXML(WxXmlModel WxXmlModel) { var QrCodeApi = new QrCodeApi(); var XML = ""; switch (WxXmlModel.MsgType) { case "text"://文本回復(fù) 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, "關(guān)注成功"); } else { XML = ResponseMessage.SubScanQrcode(WxXmlModel.FromUserName, WxXmlModel.ToUserName, WxXmlModel.EventKey);//掃描帶參數(shù)二維碼先關(guān)注后推送事件 } break; case "SCAN": XML = ResponseMessage.ScanQrcode(WxXmlModel.FromUserName, WxXmlModel.ToUserName, WxXmlModel.EventKey);//掃描帶參數(shù)二維碼已關(guān)注 直接推送事件 break; } break; default://默認(rèn)回復(fù) break; } HttpContext.Current.Response.Write(XML); HttpContext.Current.Response.End(); } #endregion
The above one is sent and the other is received, still in the WhApi.ashx handler file . I just want to make it clear, haha!
Because your handshake with the public platform was successful, you must send something to try, right~~
The picture below shows the association between a receiving method and an automatic matching reply file. Don’t worry, I will explain belowUploadThis file!
# There is still one missing configuration, that is, to set up [Debug]----[Attach to process] for vs, you only need to set the following If you check [Show all user processes], you can find w3wp.exe. If there are multiple such processes, you still have to confirm the [User Name] column, select the one with the same name as your program pool, and click OK. Attach, confirm the attachment!
Next. It’s fun……………………………………………………………………
Scan the test public account on WeChat and send a customized message to see what kind of reply there is, the above The configuration is cumbersome, and you can add breakpoints for debugging. Otherwise, there is no point in doing so much, right? Just make sure that the sending and receiving are consistent with your own settings, then it will be ok.
The above is the detailed content of Introduction to using C# WeChat development instructions. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

The history and evolution of C# and C are unique, and the future prospects are also different. 1.C was invented by BjarneStroustrup in 1983 to introduce object-oriented programming into the C language. Its evolution process includes multiple standardizations, such as C 11 introducing auto keywords and lambda expressions, C 20 introducing concepts and coroutines, and will focus on performance and system-level programming in the future. 2.C# was released by Microsoft in 2000. Combining the advantages of C and Java, its evolution focuses on simplicity and productivity. For example, C#2.0 introduced generics and C#5.0 introduced asynchronous programming, which will focus on developers' productivity and cloud computing in the future.

Methods to convert XML to JSON include: writing scripts or programs in programming languages ??(such as Python, Java, C#) to convert; pasting or uploading XML data using online tools (such as XML to JSON, Gojko's XML converter, XML online tools) and selecting JSON format output; performing conversion tasks using XML to JSON converters (such as Oxygen XML Editor, Stylus Studio, Altova XMLSpy); converting XML to JSON using XSLT stylesheets; using data integration tools (such as Informatic

C# multi-threaded programming is a technology that allows programs to perform multiple tasks simultaneously. It can improve program efficiency by improving performance, improving responsiveness and implementing parallel processing. While the Thread class provides a way to create threads directly, advanced tools such as Task and async/await can provide safer asynchronous operations and a cleaner code structure. Common challenges in multithreaded programming include deadlocks, race conditions, and resource leakage, which require careful design of threading models and the use of appropriate synchronization mechanisms to avoid these problems.

How to build applications using .NET? Building applications using .NET can be achieved through the following steps: 1) Understand the basics of .NET, including C# language and cross-platform development support; 2) Learn core concepts such as components and working principles of the .NET ecosystem; 3) Master basic and advanced usage, from simple console applications to complex WebAPIs and database operations; 4) Be familiar with common errors and debugging techniques, such as configuration and database connection issues; 5) Application performance optimization and best practices, such as asynchronous programming and caching.

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

The advantage of multithreading is that it can improve performance and resource utilization, especially for processing large amounts of data or performing time-consuming operations. It allows multiple tasks to be performed simultaneously, improving efficiency. However, too many threads can lead to performance degradation, so you need to carefully select the number of threads based on the number of CPU cores and task characteristics. In addition, multi-threaded programming involves challenges such as deadlock and race conditions, which need to be solved using synchronization mechanisms, and requires solid knowledge of concurrent programming, weighing the pros and cons and using them with caution.
