ASP.NET微信開發(fā)接口的示例分析-創(chuàng)新互聯(lián)

這篇文章給大家分享的是有關(guān)ASP.NET微信開發(fā)接口的示例分析的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

創(chuàng)新互聯(lián)長期為近千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為尖扎企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計、網(wǎng)站制作,尖扎網(wǎng)站改版等技術(shù)服務(wù)。擁有10多年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

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

ASP.NET微信開發(fā)接口的示例分析

開發(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ù)消息 
  } 
  // 自定義菜單點擊事件 
  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ù)消息 
  } 
  // 自定義菜單點擊事件 
  else if (eventType==ReqEventType.CLICK) 
  { 
   // TODO 自定義菜單權(quán)沒有開放,暫不處理該類消息 
  } 
  } 
  else if (msgType == ReqMsgType.Location) 
  { 
  } 
 
 
 } 
 catch (Exception e) 
 { 
  
 } 
 }</pre><br> 
<pre></pre> 
<br> 
<br>

感謝各位的閱讀!關(guān)于“ASP.NET微信開發(fā)接口的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

分享名稱:ASP.NET微信開發(fā)接口的示例分析-創(chuàng)新互聯(lián)
網(wǎng)站URL:http://www.muchs.cn/article8/ceodop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、動態(tài)網(wǎng)站、網(wǎng)站改版、靜態(tài)網(wǎng)站網(wǎng)站設(shè)計、網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計公司