最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ASP.NET微信開發(fā)(接口指南)

 更新時間:2015年12月02日 09:05:52   作者:yijincoldplay  
這篇文章詳細(xì)介紹了ASP.NET微信開發(fā)接口指南,微信公眾平臺的開發(fā)較為簡單,感興趣的小伙伴們可以參考一下

公眾平臺用戶提交信息后,微信服務(wù)器將發(fā)送GET請求到填寫的URL上,并且?guī)纤膫€參數(shù):

開發(fā)者通過檢驗signature對請求進(jìn)行校驗(下面有校驗方式)。若確認(rèn)此次GET請求來自微信服務(wù)器,請原樣返回echostr參數(shù)內(nèi)容,則接入生效,否則接入失敗。

signature結(jié)合了開發(fā)者填寫的token參數(shù)和請求中的timestamp參數(shù)、nonce參數(shù)。

加密/校驗流程:

  • 1. 將token、timestamp、nonce三個參數(shù)進(jìn)行字典序排序
  • 2. 將三個參數(shù)字符串拼接成一個字符串進(jìn)行sha1加密
  • 3. 開發(fā)者獲得加密后的字符串可與signature對比,標(biāo)識該請求來源于微信
/// <summary> 
 /// 驗證簽名 
 /// </summary> 
 /// <param name="signature"></param> 
 /// <param name="timestamp"></param> 
 /// <param name="nonce"></param> 
 /// <returns></returns> 
 public static bool CheckSignature(String signature, String timestamp, String nonce) 
 { 
 String[] arr = new String[] { token, timestamp, nonce }; 
 // 將token、timestamp、nonce三個參數(shù)進(jìn)行字典序排序 
 Array.Sort<String>(arr); 
 
 StringBuilder content = new StringBuilder(); 
 for (int i = 0; i < arr.Length; i++) 
 { 
  content.Append(arr[i]); 
 } 
 
 String tmpStr = SHA1_Encrypt(content.ToString()); 
 
 
 // 將sha1加密后的字符串可與signature對比,標(biāo)識該請求來源于微信 
 return tmpStr != null ? tmpStr.Equals(signature) : false; 
 } 
 
 
 /// <summary> 
 /// 使用缺省密鑰給字符串加密 
 /// </summary> 
 /// <param name="Source_String"></param> 
 /// <returns></returns> 
 public static string SHA1_Encrypt(string Source_String) 
 { 
 byte[] StrRes = Encoding.Default.GetBytes(Source_String); 
 HashAlgorithm iSHA = new SHA1CryptoServiceProvider(); 
 StrRes = iSHA.ComputeHash(StrRes); 
 StringBuilder EnText = new StringBuilder(); 
 foreach (byte iByte in StrRes) 
 { 
  EnText.AppendFormat("{0:x2}", iByte); 
 } 
 return EnText.ToString(); 
 } 

接入后是消息推送當(dāng)普通微信用戶向公眾賬號發(fā)消息時,微信服務(wù)器將POST該消息到填寫的URL上。

 protected void Page_Load(object sender, EventArgs e) 
 { 
 
 if (Request.HttpMethod.ToUpper() == "GET") 
 { 
  // 微信加密簽名 
  string signature = Request.QueryString["signature"]; 
  // 時間戳 
  string timestamp = Request.QueryString["timestamp"]; 
  // 隨機(jī)數(shù) 
  string nonce = Request.QueryString["nonce"]; 
  // 隨機(jī)字符串 
  string echostr = Request.QueryString["echostr"]; 
  if (WeixinServer.CheckSignature(signature, timestamp, nonce)) 
  { 
  Response.Write(echostr); 
  } 
 
 } 
 else if (Request.HttpMethod.ToUpper() == "POST") 
 { 
 
  StreamReader stream = new StreamReader(Request.InputStream); 
  string xml = stream.ReadToEnd(); 
 
  processRequest(xml); 
 } 
 
 
 } 
 
 
 /// <summary> 
 /// 處理微信發(fā)來的請求 
 /// </summary> 
 /// <param name="xml"></param> 
 public void processRequest(String xml) 
 { 
 try 
 { 
 
  // xml請求解析 
  Hashtable requestHT = WeixinServer.ParseXml(xml); 
 
  // 發(fā)送方帳號(open_id) 
  string fromUserName = (string)requestHT["FromUserName"]; 
  // 公眾帳號 
  string toUserName = (string)requestHT["ToUserName"]; 
  // 消息類型 
  string msgType = (string)requestHT["MsgType"]; 
 
  //文字消息 
  if (msgType == ReqMsgType.Text) 
  { 
  // Response.Write(str); 
 
  string content = (string)requestHT["Content"]; 
  if(content=="1") 
  { 
   // Response.Write(str); 
   Response.Write(GetNewsMessage(toUserName, fromUserName)); 
   return; 
  } 
  if (content == "2") 
  { 
   Response.Write(GetUserBlogMessage(toUserName, fromUserName)); 
   return; 
  } 
  if (content == "3") 
  { 
   Response.Write(GetGroupMessage(toUserName, fromUserName)); 
   return; 
  } 
  if (content == "4") 
  { 
   Response.Write(GetWinePartyMessage(toUserName, fromUserName)); 
   return; 
  } 
  Response.Write(GetMainMenuMessage(toUserName, fromUserName, "你好,我是vinehoo,")); 
 
  } 
  else if (msgType == ReqMsgType.Event) 
  { 
  // 事件類型 
  String eventType = (string)requestHT["Event"]; 
  // 訂閱 
  if (eventType==ReqEventType.Subscribe) 
  { 
   
   Response.Write(GetMainMenuMessage(toUserName, fromUserName, "謝謝您的關(guān)注!,")); 
   
  } 
  // 取消訂閱 
  else if (eventType==ReqEventType.Unsubscribe) 
  { 
   // TODO 取消訂閱后用戶再收不到公眾號發(fā)送的消息,因此不需要回復(fù)消息 
  } 
  // 自定義菜單點(diǎn)擊事件 
  else if (eventType==ReqEventType.CLICK) 
  { 
   // TODO 自定義菜單權(quán)沒有開放,暫不處理該類消息 
  } 
  } 
  else if (msgType == ReqMsgType.Location) 
  { 
  } 
 
 
 } 
 catch (Exception e) 
 { 
  
 } 
 }<pre name="code" class="csharp"> protected void Page_Load(object sender, EventArgs e) 
 { 
 
 if (Request.HttpMethod.ToUpper() == "GET") 
 { 
  // 微信加密簽名 
  string signature = Request.QueryString["signature"]; 
  // 時間戳 
  string timestamp = Request.QueryString["timestamp"]; 
  // 隨機(jī)數(shù) 
  string nonce = Request.QueryString["nonce"]; 
  // 隨機(jī)字符串 
  string echostr = Request.QueryString["echostr"]; 
  if (WeixinServer.CheckSignature(signature, timestamp, nonce)) 
  { 
  Response.Write(echostr); 
  } 
 
 } 
 else if (Request.HttpMethod.ToUpper() == "POST") 
 { 
 
  StreamReader stream = new StreamReader(Request.InputStream); 
  string xml = stream.ReadToEnd(); 
 
  processRequest(xml); 
 } 
 
 
 } 
 
 
 /// <summary> 
 /// 處理微信發(fā)來的請求 
 /// </summary> 
 /// <param name="xml"></param> 
 public void processRequest(String xml) 
 { 
 try 
 { 
 
  // xml請求解析 
  Hashtable requestHT = WeixinServer.ParseXml(xml); 
 
  // 發(fā)送方帳號(open_id) 
  string fromUserName = (string)requestHT["FromUserName"]; 
  // 公眾帳號 
  string toUserName = (string)requestHT["ToUserName"]; 
  // 消息類型 
  string msgType = (string)requestHT["MsgType"]; 
 
  //文字消息 
  if (msgType == ReqMsgType.Text) 
  { 
  // Response.Write(str); 
 
  string content = (string)requestHT["Content"]; 
  if(content=="1") 
  { 
   // Response.Write(str); 
   Response.Write(GetNewsMessage(toUserName, fromUserName)); 
   return; 
  } 
  if (content == "2") 
  { 
   Response.Write(GetUserBlogMessage(toUserName, fromUserName)); 
   return; 
  } 
  if (content == "3") 
  { 
   Response.Write(GetGroupMessage(toUserName, fromUserName)); 
   return; 
  } 
  if (content == "4") 
  { 
   Response.Write(GetWinePartyMessage(toUserName, fromUserName)); 
   return; 
  } 
  Response.Write(GetMainMenuMessage(toUserName, fromUserName, "你好,我是vinehoo,")); 
 
  } 
  else if (msgType == ReqMsgType.Event) 
  { 
  // 事件類型 
  String eventType = (string)requestHT["Event"]; 
  // 訂閱 
  if (eventType==ReqEventType.Subscribe) 
  { 
   
   Response.Write(GetMainMenuMessage(toUserName, fromUserName, "謝謝您的關(guān)注!,")); 
   
  } 
  // 取消訂閱 
  else if (eventType==ReqEventType.Unsubscribe) 
  { 
   // TODO 取消訂閱后用戶再收不到公眾號發(fā)送的消息,因此不需要回復(fù)消息 
  } 
  // 自定義菜單點(diǎn)擊事件 
  else if (eventType==ReqEventType.CLICK) 
  { 
   // TODO 自定義菜單權(quán)沒有開放,暫不處理該類消息 
  } 
  } 
  else if (msgType == ReqMsgType.Location) 
  { 
  } 
 
 
 } 
 catch (Exception e) 
 { 
  
 } 
 }</pre><br> 
<pre></pre> 
<br> 
<br> 

本文已被整理到了《ASP.NET微信開發(fā)教程匯總》,歡迎大家學(xué)習(xí)閱讀。

以上就是關(guān)于ASP.NET微信開發(fā)接口指南的相關(guān)內(nèi)容介紹,希望對大家的學(xué)習(xí)有所幫助。

相關(guān)文章

  • .NET中彈性和瞬時處理庫Polly的使用詳解

    .NET中彈性和瞬時處理庫Polly的使用詳解

    Polly 是一個 .NET 彈性和瞬態(tài)故障處理庫,允許開發(fā)人員以 Fluent 和線程安全的方式來實現(xiàn)重試、斷路、超時、隔離和回退策略,下面就跟隨小編一起來看看它的具體使用吧
    2024-01-01
  • .NET?Core項目使用swagger開發(fā)組件

    .NET?Core項目使用swagger開發(fā)組件

    這篇文章介紹了.NET?Core項目使用swagger開發(fā)組件的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • 詳解.NET Core中的數(shù)據(jù)保護(hù)組件

    詳解.NET Core中的數(shù)據(jù)保護(hù)組件

    在本篇文章中我們給大家整理了關(guān)于返回主頁.NET Core中的數(shù)據(jù)保護(hù)組件的相關(guān)知識點(diǎn)內(nèi)容,有興趣的朋友們參考下。
    2018-09-09
  • 使用EF CORE遷移數(shù)據(jù)庫

    使用EF CORE遷移數(shù)據(jù)庫

    這篇文章介紹了使用EF CORE遷移數(shù)據(jù)庫的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • asp.net上傳文件到數(shù)據(jù)庫的解決方案

    asp.net上傳文件到數(shù)據(jù)庫的解決方案

    這篇文章主要介紹了ASP.NET上傳文件到數(shù)據(jù)庫,先從文字上了解一下上傳文件到數(shù)據(jù)庫的具體步驟,再從代碼上來實現(xiàn),需要的朋友可以參考下
    2015-09-09
  • ASP.NET?MVC前臺動態(tài)添加文本框并在后臺使用FormCollection接收值

    ASP.NET?MVC前臺動態(tài)添加文本框并在后臺使用FormCollection接收值

    這篇文章介紹了ASP.NET?MVC前臺動態(tài)添加文本框并在后臺使用FormCollection接收的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-08-08
  • .net core下配置訪問數(shù)據(jù)庫操作

    .net core下配置訪問數(shù)據(jù)庫操作

    本篇文章給大家詳細(xì)分享了在.net core下配置訪問數(shù)據(jù)庫的相關(guān)操作過程以及代碼實現(xiàn)過程,有興趣的朋友參考下。
    2018-03-03
  • ADO.NET之連接池技術(shù)的使用詳解

    ADO.NET之連接池技術(shù)的使用詳解

    本篇文章是對連接池技術(shù)的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • ASP.NET性能優(yōu)化之局部緩存分析

    ASP.NET性能優(yōu)化之局部緩存分析

    如果我們在開發(fā)網(wǎng)站過程中的緩存策略是不支持頁面局部緩存的,整個架構(gòu)就是不合理的
    2011-10-10
  • .NET Core讀取配置文件

    .NET Core讀取配置文件

    這篇文章介紹了.NET Core讀取配置文件的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-07-07

最新評論

尚志市| 崇义县| 昭苏县| 竹北市| 若尔盖县| 本溪| 融水| 永定县| 敦煌市| 平陆县| 贵港市| 云南省| 新河县| 霞浦县| 马公市| 玉屏| 连山| 察隅县| 富蕴县| 寻甸| 襄城县| 商丘市| 东至县| 温泉县| 满洲里市| 阜康市| 宜都市| 红桥区| 汕头市| 同仁县| 巫山县| 长宁县| 普兰县| 高淳县| 贵溪市| 太康县| 武汉市| 徐水县| 手机| 且末县| 东城区|