微信开发asp.net的示例分析

这篇文章主要为大家展示了“微信开发asp.net的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“微信开发asp.net的示例分析”这篇文章吧。

创新互联自2013年创立以来,先为桃江等服务建站,桃江等地企业,进行企业商务咨询服务。为桃江企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

最近在接触微信开发,也有在看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><![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;
    }

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

微信开发asp.net的示例分析

以上是“微信开发asp.net的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!

新闻标题:微信开发asp.net的示例分析
地址分享:https://www.cdcxhl.com/article0/ishhio.html

成都网站建设公司_创新互联,为您提供品牌网站制作Google网站策划定制网站域名注册全网营销推广

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

绵阳服务器托管