zoukankan      html  css  js  c++  java
  • 微信消息的处理和应答

    1.微信消息应答流程
    微信服务器是客户手机和开发服务器信息流通的桥梁。消息流程图如下:

    2.微信服务器向开发服务器请求消息
    1)文本消息处理
    2)事件消息处理
    3)开发者认证处理


    微信消息处理入口操作,代码示例如下:

    public void ProcessRequest(HttpContext context)
    {
        //WHC.Framework.Commons.LogTextHelper.Info("测试记录");
    
        string postString = string.Empty;
        if (HttpContext.Current.Request.HttpMethod.ToUpper() == "POST")
        {
            using (Stream stream = HttpContext.Current.Request.InputStream)
            {
                Byte[] postBytes = new Byte[stream.Length];
                stream.Read(postBytes, 0, (Int32)stream.Length);
                postString = Encoding.UTF8.GetString(postBytes);
            }
    
            if (!string.IsNullOrEmpty(postString))
            {
                Execute(postString);
            }
        }
        else
        {
            Auth();
        }
    }

    WeixinApiDispatch消息分发管理类,它提取请求消息的内容,并构建不同类型的消息参数,传递给不同的响应函数进行处理,然后返回封装好的XML内容,作为响应。

    /// <summary>
    /// 处理各种请求信息并应答(通过POST的请求)
    /// </summary>
    /// <param name="postStr">POST方式提交的数据</param>
    private void Execute(string postStr)
    {
        WeixinApiDispatch dispatch = new WeixinApiDispatch();
        string responseContent = dispatch.Execute(postStr);
    
        HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
        HttpContext.Current.Response.Write(responseContent);
    }

    代码处理逻辑如下图所示:

    具体的消息处理类:

    /// <summary>
    /// 客户端请求的数据接口
    /// </summary>
    public interface IWeixinAction
    {
        /// <summary>
        /// 对文本请求信息进行处理
        /// </summary>
        /// <param name="info">文本信息实体</param>
        /// <returns></returns>
        string HandleText(RequestText info);
    
        /// <summary>
        /// 对图片请求信息进行处理
        /// </summary>
        /// <param name="info">图片信息实体</param>
        /// <returns></returns>
        string HandleImage(RequestImage info);
    
    ...........................
    
    
        /// <summary>
        /// 对订阅请求事件进行处理
        /// </summary>
        /// <param name="info">订阅请求事件信息实体</param>
        /// <returns></returns>
        string HandleEventSubscribe(RequestEventSubscribe info);
    
        /// <summary>
        /// 对菜单单击请求事件进行处理
        /// </summary>
        /// <param name="info">菜单单击请求事件信息实体</param>
        /// <returns></returns>
        string HandleEventClick(RequestEventClick info);
    
    ..............................
    }

    其中,实体类参数是我们根据程序开发自己定义的,继承关系如下所示:

    3.开发者服务器向微信服务器进行的消息请求消息
    我们可以通过微信,进行相关的消息回复或者数据管理操作。
    如下如所示:

    微信的回复消息处理,处理逻辑如下图所示:

    参考资料:http://www.cnblogs.com/wuhuacong/p/3614175.html

  • 相关阅读:
    bash的for循环从命令读取值
    BFS-hdu-1226-超级密码
    计算机改名导致数据库链接的诡异问题
    There is insufficient system memory to run this query 错误
    SQL Server 2008 R2的发布订阅配置实践
    MS SQL 日常维护管理常用脚本(二)
    TreeSize工具介绍
    迁移Reporting Services的方法与WMI错误
    spring-session-data-redis解决session共享的问题
    elasticSearch6源码分析(12)DiscoveryModule
  • 原文地址:https://www.cnblogs.com/shijingjing07/p/5883468.html
Copyright © 2011-2022 走看看