Home WeChat Applet WeChat Development Example analysis of C#.net implementation of WeChat public account interface development

Example analysis of C#.net implementation of WeChat public account interface development

Apr 25, 2017 am 09:24 AM
Interface development

The example in this article describes the C# WeChat public account and subscription account interface development sample code. Share it with everyone for your reference, the details are as follows:

using System;
using System.Web;
using System.IO;
using System.Text;
using System.Web.Security;
using weixin_api;
public class wxgz_api : IHttpHandler
{
  public void ProcessRequest(HttpContext context)
  {
    context.Response.ContentType = "text/plain";
    string postString = string.Empty;
    if (HttpContext.Current.Request.HttpMethod.ToUpper() == "POST")
    {
      //微信服务器对接口消息
      using (Stream stream = HttpContext.Current.Request.InputStream)
      {
        Byte[] postBytes = new Byte[stream.Length];
        stream.Read(postBytes, 0, (Int32)stream.Length);
        postString = Encoding.UTF8.GetString(postBytes);
        Handle(postString);
      }
    }
    else
    {
      //微信进行的Get测试(开发者认证)
      WxAuth();
    }
  }
  /// <summary>
  /// 处理信息并应答
  /// </summary>
  private void Handle(string postStr)
  {
    messageHelp help = new messageHelp();
    string responseContent = help.ReturnMessage(postStr);
    HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
    HttpContext.Current.Response.Write(responseContent);
  }
  #region 微信验证
  public void WxAuth()
  {
    string token = "xxxxxxxx";
    if (string.IsNullOrEmpty(token))
    {
      return;
    }
    string echoString = HttpContext.Current.Request.QueryString["echostr"];
    string signature = HttpContext.Current.Request.QueryString["signature"];
    string timestamp = HttpContext.Current.Request.QueryString["timestamp"];
    string nonce = HttpContext.Current.Request.QueryString["nonce"];
    if (CheckSignature(token, signature, timestamp, nonce))
    {
      if (!string.IsNullOrEmpty(echoString))
      {
        HttpContext.Current.Response.Write(echoString);
        HttpContext.Current.Response.End();
      }
    }
  }
  /// <summary>
  /// 验证微信签名
  /// </summary>
  public bool CheckSignature(string token, string signature, string timestamp, string nonce)
  {
    string[] ArrTmp = { token, timestamp, nonce };
    Array.Sort(ArrTmp);
    string tmpStr = string.Join("", ArrTmp);
    tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
    tmpStr = tmpStr.ToLower();
    if (tmpStr == signature)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  #endregion
  public bool IsReusable
  {
    get
    {
      return false;
    }
  }
}
Copy after login

The above is the detailed content of Example analysis of C#.net implementation of WeChat public account interface development. 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)

PHP Interface Development Guide: Creating an Enterprise WeChat Automatic Reply Function PHP Interface Development Guide: Creating an Enterprise WeChat Automatic Reply Function Sep 12, 2023 pm 02:43 PM

PHP Interface Development Guide: Building the Automatic Reply Function of Enterprise WeChat With the continuous development of Internet technology, Enterprise WeChat, as a communication tool specially created for enterprises, has been favored by more and more enterprises. However, as the scale of enterprise WeChat usage continues to expand, companies need a more efficient and smarter way to manage and respond to large amounts of information. In this context, the automatic reply function of enterprise WeChat has become an indispensable requirement. This article will take PHP interface development as the theme and introduce how to build an efficient enterprise.

How to use PHP interface to develop enterprise WeChat customer service function? How to use PHP interface to develop enterprise WeChat customer service function? Sep 11, 2023 pm 12:33 PM

How to use PHP interface to develop enterprise WeChat customer service function? Enterprise WeChat is an important platform for internal communication and collaboration within the enterprise, as well as an important channel for communication with customers. In order to provide better customer service, companies need to develop enterprise WeChat customer service functions. This article will introduce how to use the PHP interface to develop enterprise WeChat customer service functions. 1. Preparation Before starting development, you first need to register an enterprise WeChat account and create an enterprise. In the process of creating an enterprise, an enterprise WeChat application will be generated and an Agent will be obtained.

ThinkPHP6 WeChat Payment Interface Development Guide: Implementing Payment Function ThinkPHP6 WeChat Payment Interface Development Guide: Implementing Payment Function Aug 13, 2023 pm 06:40 PM

ThinkPHP6 WeChat Payment Interface Development Guide: Implementing Payment Function Introduction: With the development of the Internet, WeChat payment has become one of the indispensable payment methods in people's lives. In the process of developing web applications, integrating WeChat payment functions is an inevitable requirement. This guide will introduce how to use the ThinkPHP6 framework to develop the WeChat payment interface and implement payment functions. Part One: Preparation Before starting to write code, we need to make the following preparations: Register a WeChat official account/mini program and obtain the appi

Baidu AI Interface Guide: A must-read technical guide for Golang developers Baidu AI Interface Guide: A must-read technical guide for Golang developers Aug 25, 2023 pm 03:41 PM

Baidu AI Interface Guide: A must-read technical guide for Golang developers Introduction: With the rapid development of artificial intelligence technology, more and more developers are beginning to pay attention to and use AI interfaces to build intelligent applications. Among many AI interface providers, Baidu AI interface is widely popular for its rich functions and simplicity and ease of use. This article will use Golang as an example to provide developers with a complete guide to Baidu AI interfaces, including how to obtain and use the interfaces, and attach detailed code examples to help developers more

In-depth understanding of the application of Go language in interface development In-depth understanding of the application of Go language in interface development Mar 29, 2024 am 09:45 AM

Learn more about the application of Go language in interface development. As a fast and efficient programming language, Go language has unique advantages in interface development. Interface is an important concept in Go language. Through interface, code decoupling, flexibility improvement and code scalability can be achieved. This article will deeply explore the application of Go language in interface development, and use specific code examples to demonstrate the use of interfaces and their value in actual development. What is an interface? In Go language, an interface is an abstract type that defines the behavior of an object

Getting Started with DingTalk Interface Development: A Practical Guide to Connecting PHP and Interfaces Getting Started with DingTalk Interface Development: A Practical Guide to Connecting PHP and Interfaces Jul 06, 2023 am 09:45 AM

An Introduction to DingTalk Interface Development: A Practical Guide to Connecting PHP and Interfaces With the development of technology, the way of communication and collaboration within enterprises is also constantly changing. As an enterprise-level instant messaging and office platform, DingTalk has become the tool of choice for many companies. The development of the DingTalk interface provides enterprises with the possibility of richer functional expansion and customization needs. This article will use PHP as the main development language to help readers quickly get started with DingTalk interface development, and demonstrate how to interface with the interface through examples. Create DingTalk interface application before entering specific development practice

PHP interface development tutorial: Implementing the corporate WeChat code scanning login function PHP interface development tutorial: Implementing the corporate WeChat code scanning login function Sep 11, 2023 pm 06:50 PM

PHP interface development tutorial: Implementing the QR code scanning login function of Enterprise WeChat Preface: With the widespread use of Enterprise WeChat, many companies hope to use the QR code scanning login function provided by Enterprise WeChat to facilitate employees to log in to the system. This article will introduce how to use PHP development interface to realize the QR code login function of enterprise WeChat. 1. Apply for an enterprise WeChat developer account. First, we need to apply for an enterprise WeChat developer account. Log in to the Enterprise WeChat Developer Platform and follow the guidelines to complete account application and certification. After obtaining an enterprise WeChat developer account,

How to create API interface in PHP? How to create API interface in PHP? Jun 03, 2023 am 08:02 AM

As web development matures, the application of API interfaces becomes more and more widespread. PHP language is a very popular web development language, and its method of creating API interfaces has also attracted much attention. This article will introduce how to create an API interface in PHP, hoping to be helpful to PHP developers. 1. What is API interface? First, we need to understand the concept of API interface. API (Application Programming Interface) is an application program interface, which is a set of defined

See all articles