首页 > 微信 > 详细

微信开发asp.net

时间:2014-03-25 09:24:22      阅读:601      评论:0      收藏:0      [点我收藏+]

最近在接触微信开发,也有在看php的代码,不过最后还是使用c#语言了;

后台新建了index.ashx文件,这样速度比较快;

首先顶部引用了

using System.IO;
using System.Xml;

一个是为了实现接收xml文件流,一个是为了后面对xml文件的处理;

bubuko.com,布布扣
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();
    }
bubuko.com,布布扣

首先设置好Token,接收各种参数,请求方式是以get的方式发送;

这里主要呢是CheckSign()函数;

bubuko.com,布布扣
 1 public bool CheckSign(string signature, string timestamp, string nonce)
 2     {
 3         string[] strs = new string[] { Token, timestamp, nonce };
 4         Array.Sort(strs);//排序
 5         string strNew = string.Join("", strs);//连接成字符串
 6         strNew = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strNew, "SHA1");//加密
 7         if (signature == strNew.ToLower())
 8             return true;
 9         return false;
10     }
bubuko.com,布布扣

其实这里的意识就是接收到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;

写的不好指出哈!!

这样我们就可以对接收到的数据进行判断,做出相应的操作,最主要的还是要熟悉接口;

 

下面就一个例子说明一下,可能没有抽象的很好:

bubuko.com,布布扣
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;
    }
bubuko.com,布布扣
bubuko.com,布布扣
 1 public string transmitText(string[] xmlArray)
 2     {
 3         string xmlstring = @"<xml>
 4                             <ToUserName><![CDATA[{0}]]></ToUserName>
 5                             <FromUserName><![CDATA[{1}]]></FromUserName>
 6                             <CreateTime>{2}</CreateTime>
 7                             <MsgType><![CDATA[text]]></MsgType>
 8                             <Content><![CDATA[{3}]]></Content>
 9                             </xml>";
10         string xmlstr = string.Format(xmlstring, xmlArray);
11         return xmlstr;
12     }
bubuko.com,布布扣

这样子就完成的一个简单的回复了;

bubuko.com,布布扣

微信开发asp.net,布布扣,bubuko.com

微信开发asp.net

原文:http://www.cnblogs.com/liwon/p/3622084.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!