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") { //微信服務(wù)器對接口消息 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ā)者認(rèn)證) WxAuth(); } } /// <summary> /// 處理信息并應(yīng)答 /// </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(shè)計入門教程》及《C#程序設(shè)計之線程使用技巧總結(jié)》
希望本文所述對大家C#程序設(shè)計有所幫助。
上一篇:C#簡單判斷字符編碼的方法
欄 目:C#教程
下一篇:C#實現(xiàn)可捕獲幾乎所有鍵盤鼠標(biāo)事件的鉤子類完整實例
本文標(biāo)題:C#微信公眾號與訂閱號接口開發(fā)示例代碼
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6450.html
您可能感興趣的文章
- 01-10微信開放平臺之網(wǎng)站授權(quán)微信登錄功能
- 01-10C#身份證號碼驗證是否正確
- 01-10C#修改IIS站點framework版本號的方法
- 01-10C#獲取U盤序列號的方法
- 01-10C#有效防止同一賬號多次登錄(附三種方法)
- 01-10C#微信開發(fā)(服務(wù)器配置)
- 01-10使用C#寫了一個可以推算火車票身份證號碼的小程序
- 01-10基于C#實現(xiàn)手機號碼歸屬地接口調(diào)用
- 01-10C#解析json字符串總是多出雙引號的原因分析及解決辦法
- 01-10C#微信公眾平臺開發(fā)之高級群發(fā)接口


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻 器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機閱讀
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實例總結(jié)
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個骰子