首页

asp.net 微信公众号开发示例源码

c#

2020-9-10

主要实现了微信服务器与外站数据交换功能。

using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Text;
using System.Web.Security;
using System.Xml;
using System.Reflection;
using WeiXin.Models;

namespace WeiXinService
{
    /// <summary>
    /// 微信数据处理
    /// </summary>
    public class interfaceTest : IHttpHandler
    {
        log4net.ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);//自获取出错的类
        public void ProcessRequest(HttpContext param_context)
        {
            try
            {                
                string postString = string.Empty;
                //用户发送消息或点击等事件一般都是POST过来,微信服务器向接口发送POST请求,根据请求我们进行处理反馈
                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);
                        Handle(postString);
                    }
                }
                else
                {
                    //第一次配置接口地址的时候,微信服务器会向接口发送一个GET请求来验证你的接口地址
                    InterfaceTest();
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }

        /// <summary>
        /// 接收微信发送的XML消息并且解析
        /// </summary>
        private void ReceiveXml()
        {
            Stream requestStream = System.Web.HttpContext.Current.Request.InputStream;
            byte[] requestByte = new byte[requestStream.Length];
            requestStream.Read(requestByte, 0, (int)requestStream.Length);
            string requestStr = Encoding.UTF8.GetString(requestByte);

            if (!string.IsNullOrEmpty(requestStr))
            {
                //封装请求类
                XmlDocument requestDocXml = new XmlDocument();
                requestDocXml.LoadXml(requestStr);
                XmlElement rootElement = requestDocXml.DocumentElement;
                WxXmlModel WxXmlModel = new WxXmlModel();
                WxXmlModel.ToUserName = rootElement.SelectSingleNode("ToUserName").InnerText;
                WxXmlModel.FromUserName = rootElement.SelectSingleNode("FromUserName").InnerText;
                WxXmlModel.CreateTime = rootElement.SelectSingleNode("CreateTime").InnerText;
                WxXmlModel.MsgType = rootElement.SelectSingleNode("MsgType").InnerText;
                switch (WxXmlModel.MsgType)
                {
                    case "text"://文本
                        WxXmlModel.Content = rootElement.SelectSingleNode("Content").InnerText;
                        break;
                    case "image"://图片
                        WxXmlModel.PicUrl = rootElement.SelectSingleNode("PicUrl").InnerText;
                        break;
                    case "event"://事件
                        WxXmlModel.Event = rootElement.SelectSingleNode("Event").InnerText;
                        if (WxXmlModel.Event == "subscribe")//关注类型
                        {
                            WxXmlModel.EventKey = rootElement.SelectSingleNode("EventKey").InnerText;
                        }
                        break;
                    default:
                        break;
                }
               // ResponseXML(WxXmlModel);//回复消息
            }
        }



        /// <summary>
        /// 处理信息并应答
        /// </summary>
        private void Handle(string postStr)
        {
            messageHelp help = new messageHelp();
            string responseContent = help.ReturnMessage(postStr);
            log.Info("接收用户:"   postStr   "返回:"   responseContent);
            HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
            HttpContext.Current.Response.Write(responseContent);
        }

        /// <summary>
        /// 成为开发者url测试,返回echoStr
        /// </summary>
        public void InterfaceTest()
        {
            string token = "token";
            if (string.IsNullOrEmpty(token))
            {
                return;
            }

            //微信服务器会将下面几个参数发送到接口,接口这边返回接收到的echoStr就说明验证通过,
            //主要为了防止别人盗用你的接口,我这边没做逻辑判断直接返回接收到的echoStr来通过验证
            string echoString = HttpContext.Current.Request.QueryString["echoStr"];
            string signature = HttpContext.Current.Request.QueryString["signature"];
            string timestamp = HttpContext.Current.Request.QueryString["timestamp"];
            string nonce = HttpContext.Current.Request.QueryString["nonce"];
            log.Info("第一次配置接口:  echoString:"   echoString   ",signature:"   signature   ",timestamp:"   timestamp   ",nonce:"   nonce);

            if (!string.IsNullOrEmpty(echoString))
            {
                HttpContext.Current.Response.Write(echoString);
                HttpContext.Current.Response.End();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
资源下载此资源下载价格为4D币(VIP免费),请先
资源文件列表
WeiXinFrameWork/AccessTokenHelp.cs , 3187
WeiXinFrameWork/ApplicationInsights.config , 6051
WeiXinFrameWork/CommonHelp.cs , 597
WeiXinFrameWork/Global.asax , 90
WeiXinFrameWork/Global.asax.cs , 478
WeiXinFrameWork/Helper/MySQLHelper.cs , 12701
WeiXinFrameWork/HttpUtility.cs , 3062
WeiXinFrameWork/Models/WxXmlModel.cs , 2389
WeiXinFrameWork/Properties/AssemblyInfo.cs , 1323
WeiXinFrameWork/Properties/PublishProfiles/wxpublish.pubxml , 850
WeiXinFrameWork/Properties/PublishProfiles/wxpublish.pubxml.user , 4667
WeiXinFrameWork/TemplateMessage.cs , 2126
WeiXinFrameWork/TestWeb.aspx , 605
WeiXinFrameWork/TestWeb.aspx.cs , 1263
WeiXinFrameWork/TestWeb.aspx.designer.cs , 1479
WeiXinFrameWork/Web.Debug.config , 1245
WeiXinFrameWork/Web.Release.config , 1306
WeiXinFrameWork/Web.config , 2074
WeiXinFrameWork/WeiXin.csproj , 11489
WeiXinFrameWork/WeiXin.csproj.user , 1243
WeiXinFrameWork/WeiXin.csproj.vspscc , 257
WeiXinFrameWork/bin/ApplicationInsights.config , 6051
WeiXinFrameWork/bin/Microsoft.AI.Agent.Intercept.dll , 1839344
WeiXinFrameWork/bin/Microsoft.AI.DependencyCollector.dll , 60680
WeiXinFrameWork/bin/Microsoft.AI.DependencyCollector.xml , 60439
WeiXinFrameWork/bin/Microsoft.AI.PerfCounterCollector.dll , 67848
WeiXinFrameWork/bin/Microsoft.AI.ServerTelemetryChannel.dll , 85256
WeiXinFrameWork/bin/Microsoft.AI.ServerTelemetryChannel.xml , 57867
WeiXinFrameWork/bin/Microsoft.AI.Web.dll , 48840
WeiXinFrameWork/bin/Microsoft.AI.Web.xml , 27251
WeiXinFrameWork/bin/Microsoft.AI.WindowsServer.dll , 40168
WeiXinFrameWork/bin/Microsoft.AI.WindowsServer.xml , 25987
WeiXinFrameWork/bin/Microsoft.ApplicationInsights.dll , 162552
WeiXinFrameWork/bin/Microsoft.ApplicationInsights.xml , 159442
WeiXinFrameWork/bin/Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll , 29344
WeiXinFrameWork/bin/Microsoft.CodeDom.Providers.DotNetCompilerPlatform.xml , 1805
WeiXinFrameWork/bin/MySql.Data.dll , 456192
WeiXinFrameWork/bin/Newtonsoft.Json.dll , 700336
WeiXinFrameWork/bin/Newtonsoft.Json.xml , 707721
WeiXinFrameWork/bin/QCSoft.ERP.Bussiness.dll , 1607168
WeiXinFrameWork/bin/WeiXin.dll , 24576
WeiXinFrameWork/bin/WeiXin.dll.config , 2074
WeiXinFrameWork/bin/WeiXin.pdb , 50688
WeiXinFrameWork/bin/log4net.dll , 276480
WeiXinFrameWork/bin/log4net.xml , 1547797
WeiXinFrameWork/bin/roslyn/Microsoft.Build.Tasks.CodeAnalysis.dll , 85192
WeiXinFrameWork/bin/roslyn/Microsoft.CSharp.Core.targets , 6416
WeiXinFrameWork/bin/roslyn/Microsoft.CodeAnalysis.CSharp.dll , 4000448
WeiXinFrameWork/bin/roslyn/Microsoft.CodeAnalysis.VisualBasic.dll , 4901064
WeiXinFrameWork/bin/roslyn/Microsoft.CodeAnalysis.dll , 1724080
WeiXinFrameWork/bin/roslyn/Microsoft.VisualBasic.Core.targets , 5846
WeiXinFrameWork/bin/roslyn/System.Collections.Immutable.dll , 204544
WeiXinFrameWork/bin/roslyn/System.Reflection.Metadata.dll , 262896
WeiXinFrameWork/bin/roslyn/VBCSCompiler.exe , 63648
WeiXinFrameWork/bin/roslyn/VBCSCompiler.exe.config , 563
WeiXinFrameWork/bin/roslyn/csc.exe , 41608
WeiXinFrameWork/bin/roslyn/vbc.exe , 41608
WeiXinFrameWork/interfaceTest.ashx , 107
WeiXinFrameWork/interfaceTest.ashx.cs , 5736
WeiXinFrameWork/log/log.txt/20200111 , 1323
WeiXinFrameWork/log4net.config , 4317
WeiXinFrameWork/messageHelp.cs , 7949
WeiXinFrameWork/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache , 11352
WeiXinFrameWork/obj/Debug/TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs , 0
WeiXinFrameWork/obj/Debug/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs , 0
WeiXinFrameWork/obj/Debug/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs , 0
WeiXinFrameWork/obj/Debug/WeiXin.csproj.FileListAbsolute.txt , 5641
WeiXinFrameWork/obj/Debug/WeiXin.csprojResolveAssemblyReference.cache , 69822
WeiXinFrameWork/obj/Debug/WeiXin.dll , 24576
WeiXinFrameWork/obj/Debug/WeiXin.pdb , 50688
WeiXinFrameWork/obj/Release/DesignTimeResolveAssemblyReferencesInput.cache , 11354
WeiXinFrameWork/obj/Release/Package/PackageTmp/ApplicationInsights.config , 6051
WeiXinFrameWork/obj/Release/Package/PackageTmp/Global.asax , 90
WeiXinFrameWork/obj/Release/Package/PackageTmp/TestWeb.aspx , 605
WeiXinFrameWork/obj/Release/Package/PackageTmp/Web.config , 2076
WeiXinFrameWork/obj/Release/Package/PackageTmp/bin/ApplicationInsights.config , 6051
WeiXinFrameWork/obj/Release/Package/PackageTmp/bin/Microsoft.AI.Agent.Intercept.dll , 1839344
WeiXinFrameWork/obj/Release/Package/PackageTmp/bin/Microsoft.AI.DependencyCollector.dll , 60680
WeiXinFrameWork/obj/Release/Package/PackageTmp/bin/Microsoft.AI.PerfCounterCollector.dll , 67848
WeiXinFrameWork/obj/Release/Package/PackageTmp/bin/Microsoft.AI.ServerTelemetryChannel.dll , 85256
WeiXinFrameWork/obj/Release/Package/PackageTmp/bin/Microsoft.AI.Web.dll , 48840
WeiXinFrameWork/obj/Release/Package/PackageTmp/bin/Microsoft.AI.WindowsServer.dll , 40168
WeiXinFrameWork/obj/Release/Package/PackageTmp/bin/Microsoft.ApplicationInsights.dll , 162552
WeiXinFrameWork/obj/Release/Package/PackageTmp/bin/Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll , 29344
WeiXinFrameWork/obj/Release/Package/PackageTmp/bin/MySql.Data.dll , 456192
WeiXinFrameWork/obj/Release/Package/PackageTmp/bin/Newtonsoft.Json.dll , 700336
WeiXinFrameWork/obj/Release/Package/PackageTmp/bin/WeiXin.dll , 23040
WeiXinFrameWork/obj/Release/Package/PackageTmp/bin/log4net.dll , 276480
WeiXinFrameWork/obj/Release/Package/PackageTmp/bin/roslyn/Microsoft.Build.Tasks.CodeAnalysis.dll , 85192
WeiXinFrameWork/obj/Release/Package/PackageTmp/bin/roslyn/Microsoft.CSharp.Core.targets , 6416
WeiXinFrameWork/obj/Release/Package/PackageTmp/bin/roslyn/Microsoft.CodeAnalysis.CSharp.dll , 4000448
WeiXinFrameWork/obj/Release/Package/PackageTmp/bin/roslyn/Microsoft.CodeAnalysis.VisualBasic.dll , 4901064
WeiXinFrameWork/obj/Release/Package/PackageTmp/bin/roslyn/Microsoft.CodeAnalysis.dll , 1724080
WeiXinFrameWork/obj/Release/Package/PackageTmp/bin/roslyn/Microsoft.VisualBasic.Core.targets , 5846
WeiXinFrameWork/obj/Release/Package/PackageTmp/bin/roslyn/System.Collections.Immutable.dll , 204544
WeiXinFrameWork/obj/Release/Package/PackageTmp/bin/roslyn/System.Reflection.Metadata.dll , 262896
WeiXinFrameWork/obj/Release/Package/PackageTmp/bin/roslyn/VBCSCompiler.exe , 63648
WeiXinFrameWork/obj/Release/Package/PackageTmp/bin/roslyn/VBCSCompiler.exe.config , 563
WeiXinFrameWork/obj/Release/Package/PackageTmp/bin/roslyn/csc.exe , 41608
WeiXinFrameWork/obj/Release/Package/PackageTmp/bin/roslyn/vbc.exe , 41608
WeiXinFrameWork/obj/Release/Package/PackageTmp/interfaceTest.ashx , 107
WeiXinFrameWork/obj/Release/Package/PackageTmp/log4net.config , 4317
WeiXinFrameWork/obj/Release/Package/PackageTmp/packages.config , 1296
WeiXinFrameWork/obj/Release/Package/PackageTmp/scripts/ai.0.22.9-build00167.js , 200531
WeiXinFrameWork/obj/Release/Package/PackageTmp/scripts/ai.0.22.9-build00167.min.js , 85191
WeiXinFrameWork/obj/Release/TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs , 0
WeiXinFrameWork/obj/Release/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs , 0
WeiXinFrameWork/obj/Release/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs , 0
WeiXinFrameWork/obj/Release/TransformWebConfig/assist/Web.config , 1306
WeiXinFrameWork/obj/Release/TransformWebConfig/original/Web.config , 2074
WeiXinFrameWork/obj/Release/TransformWebConfig/transformed/Web.config , 2076
WeiXinFrameWork/obj/Release/WeiXin.csproj.FileListAbsolute.txt , 5651
WeiXinFrameWork/obj/Release/WeiXin.csprojResolveAssemblyReference.cache , 69832
WeiXinFrameWork/obj/Release/WeiXin.dll , 23040
WeiXinFrameWork/obj/Release/WeiXin.pdb , 46592
WeiXinFrameWork/obj/Release/_WPPLastBuildInfo.txt , 528
WeiXinFrameWork/packages.config , 1296
WeiXinFrameWork/scripts/ai.0.22.9-build00167.js , 200531
WeiXinFrameWork/scripts/ai.0.22.9-build00167.min.js , 85191
没有账号? 忘记密码?

社交账号快速登录