当前位置: 移动技术网 > IT编程>开发语言>.net > .NET 微信开放平台接口(接收短信、发送短信)

.NET 微信开放平台接口(接收短信、发送短信)

2018年02月17日  | 移动技术网IT编程  | 我要评论

鬼父2 上巻,快意之疯狂教师,赵杉佳雯

前两天做个项目用到了微信api功能。项目完成后经过整理封装如下微信操作类。

 

以下功能的实现需要开发者已有微信的公众平台账号,并且开发模式已开启、接口配置信息已设置完毕。废话不多说直接上代码。

 

 

 

1、公众平台账号接口配置信息中URL接口代码如下:

 

 

  1 <%@ WebHandler Language="C#" Class="WeixinInterface" %>

  2 

  3 using System;

  4 using System.Web;

  5 

  6 using WeiXin;

  7 using System.Xml;

  8 public class WeixinInterface : IHttpHandler

  9 {

 10 

 11     HttpContext context;

 12     public void ProcessRequest(HttpContext context)

 13     {

 14         this.context = context;        

 15 

 16         //WriteLog("[Begin:" + DateTime.Now.ToString() + "]");  //开始接收信息(输出日志,供调试使用)

 17 

 18         /*

 19          * 第四个参数"checkToken"为true时,此实例方法用于验证微信公众平台配置的URL、Token。

 20          * 第四个参数"checkToken"为false时,此实例方法用于接收用户信息、回复用户信息。

 21          * 第四个参数"checkToken"为true时,此实例代码最好不要包含于try catch语句中

 22          * 第三个参数从配置文件中读取,其值为微信公众平台接口配置信息中的“Token”

 23          */

 24         //第一步:实例化微信封装类 

 25         WeiXinMessage weixin = new WeiXinMessage(context.Request, context.Response, System.Configuration.ConfigurationManager.AppSettings["Token"].ToString(), false);

 26         //WriteLog("[ReceiveStr:" + reply.ReceiveStr + "]");    //输出接收字符串(输出日志,供调试使用)

 27 

 28         //第二步:获取用户发送消息类型 

 29         string msgType = weixin.GetMsgType();

 30         

 31         

 32         //第三步:根据接收到不同的消息类型,执行不同的业务逻辑

 33         if (msgType == "text")

 34         {

 35             

 36             string msg = weixin.GetMsgText();//获取用户发送文本信息

 37             //WriteLog("[UserMsg:" + msg + "]");  //输出用户发送的文本信息(输出日志,供调试使用)

 38             string answer = "";

 39             try

 40             {

 41                 //根据用户发送的信息,自动匹配自定义的“自动回复”

 42                 //answer = HEBCCC.SSSS.BLL.W_ZDHFBLL.GetAnswer(answer);

 43             }

 44             catch (Exception ex)

 45             {

 46                 WriteLog(DateTime.Now.ToString() + "[error:" + ex.Message + "]");

 47             }

 48             //WriteLog("[answer:" + answer + "]");

 49             if (!string.IsNullOrEmpty(answer))

 50             {

 51                 //匹配出自动回复内容,推送给用户文本信息

 52                 weixin.SendMsgText(answer);//此代码不能包含于try catch语句中,否则报错。

 53             }

 54             else//匹配不出自动回复内容时,从系统xml配置文件中读取默认文本内容推送给用户

 55             {

 56                 XmlNode autoReplyXmlNode = null;

 57                 try

 58                 {

 59                     string path = context.Server.MapPath("~/xml/sys.xml");

 60                     XmlDocument xmldom = new XmlDocument();

 61                     xmldom.Load(path);//加载xml文件

 62                     XmlNode xmlNode = xmldom.SelectSingleNode("root");//读取第一个节点

 63                     autoReplyXmlNode = xmlNode.SelectSingleNode("autoReply");//读取第一个节点

 64                 }

 65                 catch (Exception ex)

 66                 {

 67                     WriteLog(DateTime.Now.ToString() + "[error2:" + ex.Message + "]");

 68                 }

 69                 if (autoReplyXmlNode != null && !string.IsNullOrEmpty(autoReplyXmlNode.InnerText))

 70                     weixin.SendMsgText(autoReplyXmlNode.InnerText);//此代码不能包含于try catch语句中,否则报错。

 71 

 72             }

 73         }

 74         else if (msgType == "event")

 75         {

 76             //获取事件类型(Event)、事件Key值(EventKey)

 77             string[] array = weixin.GetEventType();

 78             if (array[0] == "click" && array[1].ToLower() == "c_khgh")

 79             {

 80                 weixin.SendMsgText("抱歉,此功能暂未开通【河北华网计算机技术有限公司】");

 81             }

 82         } 

 83     }

 84 

 85     /// <summary>

 86     /// 记录日志

 87     /// </summary>

 88     /// <param name="strMemo">内容</param>

 89     private void WriteLog(string strMemo)

 90     {

 91         string logPath=System.Configuration.ConfigurationManager.AppSettings["SysLog"].ToString();

 92         string filename = context.Server.MapPath(logPath + "\\WeiXin.txt");

 93         if (!System.IO.Directory.Exists(context.Server.MapPath(logPath)))

 94             System.IO.Directory.CreateDirectory(context.Server.MapPath(logPath));

 95         System.IO.StreamWriter sr =null;

 96         try

 97         {

 98             if (!System.IO.File.Exists(filename))

 99             {

100                 sr =  System.IO.File.CreateText(filename);

101             }

102             else

103             {

104                 sr = System.IO.File.AppendText(filename);

105             }

106             sr.WriteLine(strMemo);

107         }

108         catch

109         {

110         }

111         finally

112         {

113             if (sr != null)

114                 sr.Close();

115         }

116     }

117 

118     public bool IsReusable

119     {

120         get

121         {

122             return false;

123         }

124     }

125 

126 }

 

 

 

注:以上接口代码实现了,接收用户短信、被动向用户推送短信(自动回复)功能。由于我自己项目本身功能的要求,我只用到的接收用户文本短信、接收用户推送事件、向用户推送文本信息的功能。所以在我封装的类库里也只有这些功能。其他功能:如接收图片消息、接收地理位置消息、接收链接消息、推送音乐消息、推送图文消息并未实现。

 

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网