這篇文章主要為大家展示了“微信開發(fā)asp.net的示例分析”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“微信開發(fā)asp.net的示例分析”這篇文章吧。
后臺新建了index.ashx文件,這樣速度比較快;
首先頂部引用了
using System.IO;
using System.Xml;
一個是為了實現(xiàn)接收xml文件流,一個是為了后面對xml文件的處理;
public class index : IHttpHandler { private readonly string Token = "xxxx";//與微信公眾賬號后臺的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)有用戶想公眾賬號發(fā)送消息時觸發(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; }
其實這里的意識就是接收到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;
寫的不好指出哈!!
這樣我們就可以對接收到的數(shù)據(jù)進行判斷,做出相應(yīng)的操作,最主要的還是要熟悉接口;
下面就一個例子說明一下,可能沒有抽象的很好:
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; }
這樣子就完成的一個簡單的回復(fù)了;
以上是“微信開發(fā)asp.net的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
名稱欄目:微信開發(fā)asp.net的示例分析-創(chuàng)新互聯(lián)
本文鏈接:http://vcdvsql.cn/article38/djgipp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、建站公司、網(wǎng)頁設(shè)計公司、小程序開發(fā)、電子商務(wù)、App開發(fā)
聲明:本網(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)
猜你還喜歡下面的內(nèi)容