C#微信公众号与订阅号接口开发示例代码
作者:smartsmile2012 时间:2023-07-20 09:11:07
本文实例讲述了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#程序设计有所帮助。
标签:C#,微信
0
投稿
猜你喜欢
浅谈java实现背包算法(0-1背包问题)
2022-04-28 15:23:43
Android编程自定义View时添加自己的监听器示例
2023-08-20 11:58:27
解决Springboot项目启动后自动创建多表关联的数据库与表的方案
2023-11-24 01:11:27
Java MyBatis可视化代码生成工具使用教程
2022-12-05 15:23:39
SpringCloud HystrixDashboard服务监控详解
2021-12-16 03:35:47
Echarts+SpringMvc显示后台实时数据
2021-06-08 03:38:42
Android实现Window弹窗效果
2022-12-07 21:48:16
取消Android Studio项目与SVN关联的方法
2022-09-14 11:48:45
Android之使用Bundle进行IPC详解
2023-09-27 22:44:56
Android SQLite数据库的增 删 查找操作
2023-02-09 09:47:25
Java关于MyBatis缓存详解
2021-11-01 00:40:20
Android RIL使用详解
2021-09-30 18:06:45
Unity实现打砖块游戏
2023-12-06 04:45:04
C#中的委托、事件学习笔记
2023-01-21 18:03:49
Java跨域问题的处理详解
2021-07-05 12:55:18
Android内容提供者ContentProvider用法实例分析
2021-06-25 09:33:04
Android实现折线图小工具
2023-08-03 10:17:22
Android仿支付宝中余额宝的数字动画效果
2021-10-17 08:32:54
44条Java代码优化建议
2023-12-22 06:03:49
使用JDBC实现数据访问对象层(DAO)代码示例
2021-11-12 23:33:46