C#微信公眾號與訂閱號接口開發(fā)示例代碼
本文實例講述了C#微信公眾號與訂閱號接口開發(fā)示例代碼。分享給大家供大家參考,具體如下:
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測試(開發(fā)者認證)
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;
}
}
}
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O計入門教程》及《C#程序設計之線程使用技巧總結(jié)》
希望本文所述對大家C#程序設計有所幫助。
相關(guān)文章
C# CAD SelectionFilter下TypedValue數(shù)組使用方式
這篇文章主要介紹了C# CAD SelectionFilter下TypedValue數(shù)組使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02
.Net WInform開發(fā)筆記(二)Winform程序運行結(jié)構(gòu)圖及TCP協(xié)議在Winform中的應用
中午沒事,把去年剛畢業(yè)那會畫的幾張圖翻出來了,大概介紹Winform應用程序運行的過程,以及TCP協(xié)議在Winform中的應用。感興趣的朋友可以了解下;如果有Windows消息機制等基礎,很好理解這兩張2013-01-01
C#中的XML與JSON數(shù)據(jù)處理的案例詳解
在現(xiàn)代軟件開發(fā)中,數(shù)據(jù)交換和存儲的需求日益增長,而 XML 和 JSON 成為了兩種最常用的數(shù)據(jù)格式,它們各有特點,在不同的場景下有著各自的優(yōu)勢,本文將從 C# 的角度出發(fā),探討如何處理這兩種數(shù)據(jù)格式,并分享一些常見的問題及解決方法,需要的朋友可以參考下2024-09-09
C# Winform 實現(xiàn)屏蔽鍵盤的win和alt+F4的實現(xiàn)代碼
最近在做一個惡搞程序,就是打開后,程序獲得桌面的截圖然后,然后全屏顯示在屏幕上,用戶此時則不能進行任何操作。2009-02-02
Datagridview使用技巧(9)Datagridview的右鍵菜單
這篇文章主要為大家詳細介紹了Datagridview使用技巧,Datagridview的右鍵菜單,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05

