C#微信公众号与订阅号接口开发示例代码
作者:smartsmile2012 发布时间:2023-07-20 09:11:07
标签:C#,微信
本文实例讲述了C#微信公众号与订阅号接口开发示例代码。分享给大家供大家参考,具体如下:
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;
}
}
}
希望本文所述对大家C#程序设计有所帮助。
0
投稿
猜你喜欢
- import java.util.regex.Matcher;import java.util.regex.Pattern; /*
- 一个真实的故事大学的时候就开过一门课程,讲设计模式,可是大学生没什么编程实践经验,在大学里面听设计模式的感觉,就像听天书。听着都有道理,可是
- 如何快速构建一个Spring Boot的项目工具 ideaJDK版本 1.8Spring Boot 版本 1.5.9环境搭建实现:最基础前端
- 本文介绍了 SpringBoot之Controller的使用,分享给大家,具体如下:1.@Controller:处理http请求 2.@Re
- RocketMq消息处理RocketMq消息处理整个流程如下:本系列RocketMQ4.8注释github地址,希望对大家有所帮助,要是觉得
- try &
- @Profile注解详解@Profile:Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能;开发环境develop
- 前言本文主要跟大家介绍了关于Java用gson解析Json的相关内容,分享出来供大家参考学习,需要的朋友们下面来一起看看吧。json数据{&
- 我们用NuGet还原.NET Core项目会报以下错误:error NETSDK1064: 未找到版本为 1.8.2 的包 BouncyCa
- 这篇文章主要介绍了SpringBoot2整合activiti6环境搭建过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定
- 本文实例讲述了C#实现的JS操作类。分享给大家供大家参考。具体如下:这个C#类封装了常用的JS客户端代码操作,包括弹出对话框、返回上一页,通
- 网络工具类NetworkUtils,供大家参考,具体内容如下提供的方法:打开网络设置界面 openWirelessSettings判断网络是
- BigDecimal 和 0 比较大小调用BigDecimal中的compareTo方法, 如:int i = bigDecimal.com
- graylog配置springboot配置依赖compile group: 'de.siegmar', name: '
- 什么是枚举?枚举是JDK5引入的新特性。在某些情况下,一个类的对象是固定的,就可以定义为枚举。在实际使用中,枚举类型也可以作为一种规范,保障
- Java中的main函数的详细介绍JAVA中的主函数是我们再熟悉不过的了,相信每个学习过JAVA语言的人都能够熟练地写出这个程序的入口函数,
- 一、redis key数量为1千万时。存储value为"0",比较小。如果value较大,则存储内存会增多redis k
- 系统原来用的是BOSCH_BMA222的gsensor, 现在要求换成使用MMA7660,我们来看一下怎样增加驱动和调试过程。 1. 修改M
- 一般而言,Android 应用在请求数据时都是以 Get 或 Post 等方式向远程服务器发起请求,那你有没有想过其实我们也可以在 Andr
- 本文实例为大家分享了Java实现图形界面计算器的具体代码,供大家参考,具体内容如下 代码:import javax.swing.*