Home WeChat Applet WeChat Development Introduction to using C# WeChat development instructions

Introduction to using C# WeChat development instructions

Mar 15, 2017 pm 05:05 PM
c# WeChat development

This 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>
 /// 验证微信签名
 /// </summary>
 /// <returns></returns>
 /// * 将token、timestamp、nonce三个参数进行字典序排序
 /// * 将三个参数字符串拼接成一个字符串进行sha1加密
 /// * 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。
 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;
 }
Copy after login

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

Introduction to using C# WeChat development instructions

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>
 /// 接收微信发送的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))
 {
 //封装请求类
 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")//关注类型
  {
  wxXmlModel.EventKey = rootElement.SelectSingleNode("EventKey").InnerText;
  }
  break;
  default:
  break;
 }

 ResponseXML(wxXmlModel);//回复消息
 }
 }
 #endregion

 #region 回复消息
 private void ResponseXML(WxXmlModel WxXmlModel)
 {
 var QrCodeApi = new QrCodeApi();
 var XML = "";
 switch (WxXmlModel.MsgType)
 {
 case "text"://文本回复
  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, "关注成功");
  }
  else
  {
  XML = ResponseMessage.SubScanQrcode(WxXmlModel.FromUserName, WxXmlModel.ToUserName, WxXmlModel.EventKey);//扫描带参数二维码先关注后推送事件
  }
  break;
  case "SCAN":
  XML = ResponseMessage.ScanQrcode(WxXmlModel.FromUserName, WxXmlModel.ToUserName, WxXmlModel.EventKey);//扫描带参数二维码已关注 直接推送事件
  break;
  }
  break;
 default://默认回复
  break;
 }
 HttpContext.Current.Response.Write(XML);
 HttpContext.Current.Response.End();
 }
 #endregion
Copy after login

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!

Introduction to using C# WeChat development instructions

# 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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

C# Serialization C# Serialization Sep 03, 2024 pm 03:30 PM

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Factorial in C# Factorial in C# Sep 03, 2024 pm 03:34 PM

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Prime Numbers in C# Prime Numbers in C# Sep 03, 2024 pm 03:35 PM

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

The difference between multithreading and asynchronous c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

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.

See all articles