zoukankan      html  css  js  c++  java
  • Web API实现微信公众平台开发-接收数据Post

    介绍

    当普通微信用户向公众账号发消息时,微信服务器将POST消息的XML数据包到开发者填写的URL上。在微信用户和公众号产生交互的过程中,用户的某些操作会使得微信服务器通过事件推送的形式通知到开发者在开发者中心处设置的服务器地址,从而开发者可以获取到该信息。其中,某些事件推送在发生后,是允许开发者回复用户的,某些则不允许。

    代码实现

    1、接收参数。

    var postModel = GetPostModel(request);

    2、验证签名。

     if (!CheckSignature.Check(postModel.Signature, postModel.Timestamp, postModel.Nonce, Token))
                    {
                        return new HttpResponseMessage
                        {
                            Content = new StringContent("参数错误!")
                        };
                    }

    3、数据处理。

     [HttpPost]
            [ActionName("CallBack")]
            public HttpResponseMessage HandleMsgFromWeChat(HttpRequestMessage request)
            {
                try
                {
                    var postModel = GetPostModel(request);
                    if (!CheckSignature.Check(postModel.Signature, postModel.Timestamp, postModel.Nonce, Token))
                    {
                        return new HttpResponseMessage
                        {
                            Content = new StringContent("参数错误!")
                        };
                    }
                    string xmlContent = request.Content.ReadAsStringAsync().Result;
    
                    string domain = BqoolWebSite.WebSiteMapping[BqoolWebSiteType.BigCRMWebService][CommonSetting.GetEnvironmentMode()];
                    string url = domain + "/api/WeChat/ProcessXml";
                    var data = new ProcessXmlParam()
                    {
                        Account = "WeChat",
                        UpdateUser = "WeChat",
                        Date = DateTime.UtcNow.ToUnixTimestamp(),
                        Action = BaseRequestParam.ActionType.Add,
                        xml = xmlContent
                    };
                    _nLogger.Info($"WeChat ProcessXml Call BigCRM Web - Url : {url}, data : {data.ToJsonString()}");
                    var result = HttpTools.EncryptPost<ProcessXmlParam, ApiResult>(url, data);
                    _nLogger.Info($"WeChat ProcessXml Call BigCRM Web Result - Url : {url}, result : {result.ToJsonString()}");
                    if (result.Success && result.Data != null)
                    {
                        return new HttpResponseMessage
                        {
                            Content = new StringContent(result.Data.ToString())
                        };
                    }
                    return new HttpResponseMessage
                    {
                        Content = new StringContent("错误!")
                    };
                }
                catch (Exception ex)
                {
                    _nLogger.Error(ex);
                    return new HttpResponseMessage
                    {
                        Content = new StringContent("")
                    };
                }
            }
    
            /// <summary>
            /// 验签参数
            /// </summary>
            /// <param name="content"></param>
            /// <returns></returns>
            private PostModel GetPostModel(HttpRequestMessage content)
            {
                string signature = (from kvp in content.GetQueryNameValuePairs()
                                    where kvp.Key == "signature"
                                    select kvp.Value).FirstOrDefault();
    
                string timestamp = (from kvp in content.GetQueryNameValuePairs()
                                    where kvp.Key == "timestamp"
                                    select kvp.Value).FirstOrDefault();
    
                string nonce = (from kvp in content.GetQueryNameValuePairs()
                                where kvp.Key == "nonce"
                                select kvp.Value).FirstOrDefault();
    
                return new PostModel()
                {
                    Signature = signature,
                    Timestamp = timestamp,
                    Nonce = nonce
                };
            }

    总结

    其实整个api过程是很简单的,主要的是自己业务逻辑的处理。

  • 相关阅读:
    性能测试之开源的性能监控软件
    POJ 2553 The Bottom of a Graph TarJan算法题解
    Spring AOP切面
    [2014.5.22][UBUNTU]Ubuntu与Windows系统时间不同步的问题
    MySQL bug:server-id默认被自己主动置为1
    [Android]Fragment源代码分析(二) 状态
    window nginx 启动无提示错误,却没有listen 80port
    Shell 命令--文件创建、搜索命令--总结自《Linux Shell 脚本攻略》
    freemarker 自己定义指令
    javascript Deferred和递归次数限制
  • 原文地址:https://www.cnblogs.com/lyl6796910/p/14391793.html
Copyright © 2011-2022 走看看