首頁  >  文章  >  微信小程式  >  微信開發asp.net

微信開發asp.net

高洛峰
高洛峰原創
2017-02-23 13:51:541499瀏覽

最近在接觸微信開發,也有在看php的程式碼,不過最後還是使用c#語言了;

在後台新建了index.ashx文件,這樣速度比較快;

首先頂部引用了

using System.IO;
using System.Xml;

#一個是為了實作接收xml檔案流,一個是為了後面對xml檔案的處理;

public class index : IHttpHandler {

    private readonly string Token = "xxxx";//与微信公众账号后台的Token设置保持一致,区分大小写。
    
    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 - 当有用户想公众账号发送消息时触发,写事件
        }

        context.Response.End();
    }

首先設定好Token,接收各種參數,請求方式是以get的方式發送;

這裡主要呢是CheckSign()函數;

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;

寫的不好指出哈! !

這樣我們就可以對接收到的資料進行判斷,做出對應的操作,最主要的還是要熟悉介面;

 

下面就一個例子說明一下,可能沒有抽象的很好:

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 = "欢迎关注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></tousername>
                            <fromusername></fromusername>
                            <createtime>{2}</createtime>
                            <msgtype></msgtype>
                            <content></content>
                            </xml>";
        string xmlstr = string.Format(xmlstring, xmlArray);
        return xmlstr;
    }

這樣子就完成的一個簡單的回復了;

微信開發asp.net


##更多微信開發asp.net相關文章請關注PHP中文網!


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn