這篇文章主要為大家展示了“微信開發(fā)asp.net的示例分析”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“微信開發(fā)asp.net的示例分析”這篇文章吧。
創(chuàng)新互聯(lián)堅(jiān)信:善待客戶,將會(huì)成為終身客戶。我們能堅(jiān)持多年,是因?yàn)槲覀円恢笨芍档眯刨嚒N覀儚牟缓鲇瞥踉L客戶,我們用心做好本職工作,不忘初心,方得始終。十載網(wǎng)站建設(shè)經(jīng)驗(yàn)創(chuàng)新互聯(lián)是成都老牌網(wǎng)站營銷服務(wù)商,為您提供網(wǎng)站設(shè)計(jì)制作、成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、H5高端網(wǎng)站建設(shè)、網(wǎng)站制作、高端網(wǎng)站設(shè)計(jì)、小程序定制開發(fā)服務(wù),給眾多知名企業(yè)提供過好品質(zhì)的建站服務(wù)。后臺(tái)新建了index.ashx文件,這樣速度比較快;
首先頂部引用了
using System.IO;
using System.Xml;
一個(gè)是為了實(shí)現(xiàn)接收xml文件流,一個(gè)是為了后面對xml文件的處理;
public class index : IHttpHandler { private readonly string Token = "xxxx";//與微信公眾賬號(hào)后臺(tái)的Token設(shè)置保持一致,區(qū)分大小寫。 public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; string signature = context.Request["signature"]; string timestamp = context.Request["timestamp"]; string nonce = context.Request["nonce"]; string echostr = context.Request["echostr"]; if (context.Request.HttpMethod == "GET") { if (CheckSign(signature, timestamp, nonce)) { context.Response.Output.Write(echostr); } } else { //post method - 當(dāng)有用戶想公眾賬號(hào)發(fā)送消息時(shí)觸發(fā),寫事件 } context.Response.End(); }
首先設(shè)置好Token,接收各種參數(shù),請求方式是以get的方式發(fā)送;
這里主要呢是CheckSign()函數(shù);
public bool CheckSign(string signature, string timestamp, string nonce) { string[] strs = new string[] { Token, timestamp, nonce }; Array.Sort(strs);//排序 string strNew = string.Join("", strs);//連接成字符串 strNew = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strNew, "SHA1");//加密 if (signature == strNew.ToLower()) return true; return false; }
其實(shí)這里的意識(shí)就是接收到A/B/C/D,E為自定義,B/C/E生成F,與A比較,相等返回輸出D;
string xmlFromWeChat = new StreamReader(context.Request.InputStream).ReadToEnd();//讀取XML流 XmlDocument xmldocument = new XmlDocument(); xmldocument.LoadXml(xmlFromWeChat);加載字符串 string fromContent = xmldocument.GetElementsByTagName("Content").Item(0).InnerText; string fromMsgType = xmldocument.GetElementsByTagName("MsgType").Item(0).InnerText;
寫的不好指出哈?。?/p>
這樣我們就可以對接收到的數(shù)據(jù)進(jìn)行判斷,做出相應(yīng)的操作,最主要的還是要熟悉接口;
下面就一個(gè)例子說明一下,可能沒有抽象的很好:
public string receiveText(string xmlFromWeChat) { XmlDocument xmlText = new XmlDocument(); xmlText.LoadXml(xmlFromWeChat); string content; string xmlStr; string keyword = xmlText.GetElementsByTagName("Content").Item(0).InnerText.Trim(); content = "歡迎關(guān)注xxx!"; string[] defArray = { xmlText.GetElementsByTagName("FromUserName").Item(0).InnerText, xmlText.GetElementsByTagName("ToUserName").Item(0).InnerText, ConvertDateTimeInt(DateTime.Now).ToString(), content}; xmlStr = transmitText(defArray); } return xmlStr; }
public string transmitText(string[] xmlArray) { string xmlstring = @"<xml> <ToUserName><![CDATA[{0}]]></ToUserName> <FromUserName><![CDATA[{1}]]></FromUserName> <CreateTime>{2}</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[{3}]]></Content> </xml>"; string xmlstr = string.Format(xmlstring, xmlArray); return xmlstr; }
這樣子就完成的一個(gè)簡單的回復(fù)了;
以上是“微信開發(fā)asp.net的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
名稱欄目:微信開發(fā)asp.net的示例分析-創(chuàng)新互聯(lián)
本文鏈接:http://muchs.cn/article38/djgipp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、建站公司、網(wǎng)頁設(shè)計(jì)公司、小程序開發(fā)、電子商務(wù)、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容