音效素材网提供各类素材,打造精品素材网站!

站内导航 站长工具 投稿中心 手机访问

音效素材

C#.net 微信公众账号接口开发
日期:2021-09-07 22:12:52   来源:脚本之家

微信越来越火,微信公众平台成为开发成新宠,本文用C#.net开发微信公众信号接口。

微信接口地址代码:

weixin _wx = new weixin();

        string postStr = "";

        if (Request.HttpMethod.ToLower() == "post")

        {

          Stream s = System.Web.HttpContext.Current.Request.InputStream;

          byte[] b = new byte[s.Length];

          s.Read(b, 0, (int)s.Length);

          postStr = Encoding.UTF8.GetString(b);


          if (!string.IsNullOrEmpty(postStr)) //请求处理

          {

            _wx.Handle(postStr); 

          }

        }

        else

        {

          _wx.Auth();

        }


微信处理类:

public class weixin

  {

    private string Token = "weixin_token"; //换成自己的token

     

    public void Auth()

    {

      string echoStr = System.Web.HttpContext.Current.Request.QueryString["echoStr"];

      if (CheckSignature())

      {

        if (!string.IsNullOrEmpty(echoStr))

        {

          System.Web.HttpContext.Current.Response.Write(echoStr);

          System.Web.HttpContext.Current.Response.End();

        }

      }

    }


    public void Handle(string postStr)

    {

      //封装请求类

      XmlDocument doc = new XmlDocument();

      doc.LoadXml(postStr);

      XmlElement rootElement = doc.DocumentElement;


      XmlNode MsgType = rootElement.SelectSingleNode("MsgType");


      RequestXML requestXML = new RequestXML();

      requestXML.ToUserName = rootElement.SelectSingleNode("ToUserName").InnerText;

      requestXML.FromUserName = rootElement.SelectSingleNode("FromUserName").InnerText;

      requestXML.CreateTime = rootElement.SelectSingleNode("CreateTime").InnerText;

      requestXML.MsgType = MsgType.InnerText;


      if (requestXML.MsgType == "text")

      {

        requestXML.Content = rootElement.SelectSingleNode("Content").InnerText;

      }

      else if (requestXML.MsgType == "location")

      {

        requestXML.Location_X = rootElement.SelectSingleNode("Location_X").InnerText;

        requestXML.Location_Y = rootElement.SelectSingleNode("Location_Y").InnerText;

        requestXML.Scale = rootElement.SelectSingleNode("Scale").InnerText;

        requestXML.Label = rootElement.SelectSingleNode("Label").InnerText;

      }

      else if (requestXML.MsgType == "image")

      {

        requestXML.PicUrl = rootElement.SelectSingleNode("PicUrl").InnerText;

      }


      //回复消息

      ResponseMsg(requestXML);

    }


    /// <summary>

    /// 验证微信签名

    /// </summary>

    /// * 将token、timestamp、nonce三个参数进行字典序排序

    /// * 将三个参数字符串拼接成一个字符串进行sha1加密

    /// * 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。

    /// <returns></returns>

    private bool CheckSignature()

    {

      string signature = System.Web.HttpContext.Current.Request.QueryString["signature"];

      string timestamp = System.Web.HttpContext.Current.Request.QueryString["timestamp"];

      string nonce = System.Web.HttpContext.Current.Request.QueryString["nonce"];

      string[] ArrTmp = { Token, timestamp, nonce };

      Array.Sort(ArrTmp);   //字典排序

      string tmpStr = string.Join("", ArrTmp);

      tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");

      tmpStr = tmpStr.ToLower();

      if (tmpStr == signature)

      {

        return true;

      }

      else

      {

        return false;

      }

    }

    

    /// <summary>

    /// 回复消息(微信信息返回)

    /// </summary>

    /// <param name="weixinXML"></param>

    private void ResponseMsg(RequestXML requestXML)

    {

      try

      {

        string resxml = "";

        mijiya mi = new mijiya(requestXML.Content,requestXML.FromUserName);


        if (requestXML.MsgType == "text")

        {

          //在这里执行一系列操作,从而实现自动回复内容. 

          string _reMsg = mi.GetReMsg();

          if (mi.msgType == 1)

          {

            resxml = "<xml><ToUserName><![CDATA[" + requestXML.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + requestXML.ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[news]]></MsgType><Content><![CDATA[]]></Content><ArticleCount>2</ArticleCount><Articles>";

            resxml += mi.GetRePic(requestXML.FromUserName);

            resxml += "</Articles><FuncFlag>1</FuncFlag></xml>";

          }

          else

          {

            resxml = "<xml><ToUserName><![CDATA[" + requestXML.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + requestXML.ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[" + _reMsg + "]]></Content><FuncFlag>1</FuncFlag></xml>";

          }

        }

        else if (requestXML.MsgType == "location")

        {

          string city = GetMapInfo(requestXML.Location_X, requestXML.Location_Y);

          if (city == "0")

          {

            resxml = "<xml><ToUserName><![CDATA[" + requestXML.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + requestXML.ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[好啦,我们知道您的位置啦。您可以:" + mi.GetDefault() + "]]></Content><FuncFlag>1</FuncFlag></xml>";

          }

          else

          {

            resxml = "<xml><ToUserName><![CDATA[" + requestXML.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + requestXML.ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[好啦,我们知道您的位置啦。您可以:" + mi.GetDefault() + "]]></Content><FuncFlag>1</FuncFlag></xml>";

          }

        }

        else if (requestXML.MsgType == "image")

        {

          resxml = "<xml><ToUserName><![CDATA[" + requestXML.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + requestXML.ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[亲,我没有看懂你的意思。您可以:" + mi.GetDefault() + "]]></Content><FuncFlag>1</FuncFlag></xml>";

          //返回10以内条

          //int size = 10;

          //resxml = "<xml><ToUserName><![CDATA[" + requestXML.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + requestXML.ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[news]]></MsgType><Content><![CDATA[]]></Content><ArticleCount>" + size + "</ArticleCount><Articles>";

          //List<string> list = new List<string>();

          ////假如有20条查询的返回结果

          //for (int i = 0; i < 20; i++)

          //{

          //  list.Add("1");

          //}

          //string[] piclist = new string[] { "/Abstract_Pencil_Scribble_Background_Vector_main.jpg", "/balloon_tree.jpg", "/bloom.jpg", "/colorful_flowers.jpg", "/colorful_summer_flower.jpg", "/fall.jpg", "/fall_tree.jpg", "/growing_flowers.jpg", "/shoes_illustration.jpg", "/splashed_tree.jpg" };


          //for (int i = 0; i < size && i < list.Count; i++)

          //{

          //  resxml += "<item><Title><![CDATA[沈阳-黑龙江]]></Title><Description><![CDATA[元旦特价:¥300 市场价:¥400]]></Description><PicUrl><![CDATA[" + "http://www.hougelou.com" + piclist[i] + "]]></PicUrl><Url><![CDATA[http://www.hougelou.com]]></Url></item>";

          //}

          //resxml += "</Articles><FuncFlag>1</FuncFlag></xml>";

        }

        else if (wx_tmsg.GetMsgCount(requestXML.FromUserName) == 0)

        {

          resxml = "<xml><ToUserName><![CDATA[" + requestXML.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + requestXML.ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[" + mi.GetFirst() + "]]></Content><FuncFlag>1</FuncFlag></xml>";

          

        }

        else

        {

          resxml = "<xml><ToUserName><![CDATA[" + requestXML.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + requestXML.ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[亲,我没有看懂你的意思。您可以:" + mi.GetDefault() + "]]></Content><FuncFlag>1</FuncFlag></xml>";

          

        }

        //WriteTxt(resxml);

        System.Web.HttpContext.Current.Response.Write(resxml);


        WriteToDB(requestXML, resxml,mi.pid);

      }

      catch (Exception ex)

      {

        //WriteTxt("异常:" + ex.Message + "Struck:" + ex.StackTrace.ToString());

        wx_logs.MyInsert("异常:" + ex.Message + "Struck:" + ex.StackTrace.ToString());

      }

    }

     

    /// <summary>

    /// unix时间转换为datetime

    /// </summary>

    /// <param name="timeStamp"></param>

    /// <returns></returns>

    private DateTime UnixTimeToTime(string timeStamp)

    {

      DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));

      long lTime = long.Parse(timeStamp + "0000000");

      TimeSpan toNow = new TimeSpan(lTime);

      return dtStart.Add(toNow);

    }


    /// <summary>

    /// datetime转换为unixtime

    /// </summary>

    /// <param name="time"></param>

    /// <returns></returns>

    private int ConvertDateTimeInt(System.DateTime time)

    {

      System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));

      return (int)(time - startTime).TotalSeconds;

    }


    /// <summary>

    /// 调用百度地图,返回坐标信息

    /// </summary>

    /// <param name="y">经度</param>

    /// <param name="x">纬度</param>

    /// <returns></returns>

    public string GetMapInfo(string x, string y)

    {

      try

      {

        string res = string.Empty;

        string parame = string.Empty;

        string url = "http://maps.googleapis.com/maps/api/geocode/xml";

        parame = "latlng=" + x + "," + y + "&language=zh-CN&sensor=false";//此key为个人申请

        res = webRequestPost(url, parame);


        XmlDocument doc = new XmlDocument();


        doc.LoadXml(res);

        XmlElement rootElement = doc.DocumentElement;

        string Status = rootElement.SelectSingleNode("status").InnerText;

        if (Status == "OK")

        {

          //仅获取城市

          XmlNodeList xmlResults = rootElement.SelectSingleNode("/GeocodeResponse").ChildNodes;

          for (int i = 0; i < xmlResults.Count; i++)

          {

            XmlNode childNode = xmlResults[i];

            if (childNode.Name == "status")

            {

              continue;

            }


            string city = "0";

            for (int w = 0; w < childNode.ChildNodes.Count; w++)

            {

              for (int q = 0; q < childNode.ChildNodes[w].ChildNodes.Count; q++)

              {

                XmlNode childeTwo = childNode.ChildNodes[w].ChildNodes[q];


                if (childeTwo.Name == "long_name")

                {

                  city = childeTwo.InnerText;

                }

                else if (childeTwo.InnerText == "locality")

                {

                  return city;

                }

              }

            }

            return city;

          }

        }

      }

      catch (Exception ex)

      {

        //WriteTxt("map异常:" + ex.Message.ToString() + "Struck:" + ex.StackTrace.ToString());

        return "0";

      }


      return "0";

    }


    /// <summary>

    /// Post 提交调用抓取

    /// </summary>

    /// <param name="url">提交地址</param>

    /// <param name="param">参数</param>

    /// <returns>string</returns>

    public string webRequestPost(string url, string param)

    {

      byte[] bs = System.Text.Encoding.UTF8.GetBytes(param);


      HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url + "?" + param);

      req.Method = "Post";

      req.Timeout = 120 * 1000;

      req.ContentType = "application/x-www-form-urlencoded;";

      req.ContentLength = bs.Length;


      using (Stream reqStream = req.GetRequestStream())

      {

        reqStream.Write(bs, 0, bs.Length);

        reqStream.Flush();

      }

      using (WebResponse wr = req.GetResponse())

      {

        //在这里对接收到的页面内容进行处理 


        Stream strm = wr.GetResponseStream();


        StreamReader sr = new StreamReader(strm, System.Text.Encoding.UTF8);


        string line;


        System.Text.StringBuilder sb = new System.Text.StringBuilder();


        while ((line = sr.ReadLine()) != null)

        {

          sb.Append(line + System.Environment.NewLine);

        }

        sr.Close();

        strm.Close();

        return sb.ToString();

      }

    }


    private void WriteToDB(RequestXML requestXML,string _xml,int _pid)

    {

      wx_tmsg wx = new wx_tmsg();

      wx.AddNew();

      wx.FromUserName = requestXML.FromUserName;

      wx.ToUserName = requestXML.ToUserName;

      wx.MsgType = requestXML.MsgType;

      wx.Msg = requestXML.Content;

      wx.Creatime = requestXML.CreateTime;

      wx.Location_X = requestXML.Location_X;

      wx.Location_Y = requestXML.Location_Y;

      wx.Label = requestXML.Label;

      wx.Scale = requestXML.Scale;

      wx.PicUrl = requestXML.PicUrl;

      wx.reply = _xml;

      wx.pid = _pid;

      try

      {

        wx.Update();

      }

      catch (Exception ex)

      {

        wx_logs.MyInsert(ex.Message);

        //ex.message;

      }


    }

  }


  //微信请求类

  public class RequestXML

  {

    private string toUserName="";

    /// <summary>

    /// 消息接收方微信号,一般为公众平台账号微信号

    /// </summary>

    public string ToUserName

    {

      get { return toUserName; }

      set { toUserName = value; }

    }


    private string fromUserName = "";

    /// <summary>

    /// 消息发送方微信号

    /// </summary>

    public string FromUserName

    {

      get { return fromUserName; }

      set { fromUserName = value; }

    }


    private string createTime = "";

    /// <summary>

    /// 创建时间

    /// </summary>

    public string CreateTime

    {

      get { return createTime; }

      set { createTime = value; }

    }


    private string msgType = "";

    /// <summary>

    /// 信息类型 地理位置:location,文本消息:text,消息类型:image

    /// </summary>

    public string MsgType

    {

      get { return msgType; }

      set { msgType = value; }

    }


    private string content = "";

    /// <summary>

    /// 信息内容

    /// </summary>

    public string Content

    {

      get { return content; }

      set { content = value; }

    }


    private string location_X = "";

    /// <summary>

    /// 地理位置纬度

    /// </summary>

    public string Location_X

    {

      get { return location_X; }

      set { location_X = value; }

    }


    private string location_Y = "";

    /// <summary>

    /// 地理位置经度

    /// </summary>

    public string Location_Y

    {

      get { return location_Y; }

      set { location_Y = value; }

    }


    private string scale = "";

    /// <summary>

    /// 地图缩放大小

    /// </summary>

    public string Scale

    {

      get { return scale; }

      set { scale = value; }

    }


    private string label = "";

    /// <summary>

    /// 地理位置信息

    /// </summary>

    public string Label

    {

      get { return label; }

      set { label = value; }

    }


    private string picUrl = "";

    /// <summary>

    /// 图片链接,开发者可以用HTTP GET获取

    /// </summary>

    public string PicUrl

    {

      get { return picUrl; }

      set { picUrl = value; }

    }

}

说明: 其中用到的mijiya类,主要是调用数据库进行关键词匹配自动回复内容,可以根据自己的业务情况编写。

    您感兴趣的教程

    在docker中安装mysql详解

    本篇文章主要介绍了在docker中安装mysql详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编...

    详解 安装 docker mysql

    win10中文输入法仅在桌面显示怎么办?

    win10中文输入法仅在桌面显示怎么办?

    win10系统使用搜狗,QQ输入法只有在显示桌面的时候才出来,在使用其他程序输入框里面却只能输入字母数字,win10中...

    win10 中文输入法

    一分钟掌握linux系统目录结构

    这篇文章主要介绍了linux系统目录结构,通过结构图和多张表格了解linux系统目录结构,感兴趣的小伙伴们可以参考一...

    结构 目录 系统 linux

    PHP程序员玩转Linux系列 Linux和Windows安装

    这篇文章主要为大家详细介绍了PHP程序员玩转Linux系列文章,Linux和Windows安装nginx教程,具有一定的参考价值,感兴趣...

    玩转 程序员 安装 系列 PHP

    win10怎么安装杜比音效Doby V4.1 win10安装杜

    第四代杜比®家庭影院®技术包含了一整套协同工作的技术,让PC 发出清晰的环绕声同时第四代杜比家庭影院技术...

    win10杜比音效

    纯CSS实现iOS风格打开关闭选择框功能

    这篇文章主要介绍了纯CSS实现iOS风格打开关闭选择框,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作...

    css ios c

    Win7如何给C盘扩容 Win7系统电脑C盘扩容的办法

    Win7如何给C盘扩容 Win7系统电脑C盘扩容的

    Win7给电脑C盘扩容的办法大家知道吗?当系统分区C盘空间不足时,就需要给它扩容了,如果不管,C盘没有足够的空间...

    Win7 C盘 扩容

    百度推广竞品词的投放策略

    SEM是基于关键词搜索的营销活动。作为推广人员,我们所做的工作,就是打理成千上万的关键词,关注它们的质量度...

    百度推广 竞品词

    Visual Studio Code(vscode) git的使用教程

    这篇文章主要介绍了详解Visual Studio Code(vscode) git的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。...

    教程 Studio Visual Code git

    七牛云储存创始人分享七牛的创立故事与

    这篇文章主要介绍了七牛云储存创始人分享七牛的创立故事与对Go语言的应用,七牛选用Go语言这门新兴的编程语言进行...

    七牛 Go语言

    Win10预览版Mobile 10547即将发布 9月19日上午

    微软副总裁Gabriel Aul的Twitter透露了 Win10 Mobile预览版10536即将发布,他表示该版本已进入内部慢速版阶段,发布时间目...

    Win10 预览版

    HTML标签meta总结,HTML5 head meta 属性整理

    移动前端开发中添加一些webkit专属的HTML5头部标签,帮助浏览器更好解析HTML代码,更好地将移动web前端页面表现出来...

    移动端html5模拟长按事件的实现方法

    这篇文章主要介绍了移动端html5模拟长按事件的实现方法的相关资料,小编觉得挺不错的,现在分享给大家,也给大家...

    移动端 html5 长按

    HTML常用meta大全(推荐)

    这篇文章主要介绍了HTML常用meta大全(推荐),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参...

    cdr怎么把图片转换成位图? cdr图片转换为位图的教程

    cdr怎么把图片转换成位图? cdr图片转换为

    cdr怎么把图片转换成位图?cdr中插入的图片想要转换成位图,该怎么转换呢?下面我们就来看看cdr图片转换为位图的...

    cdr 图片 位图

    win10系统怎么录屏?win10系统自带录屏详细教程

    win10系统怎么录屏?win10系统自带录屏详细

    当我们是使用win10系统的时候,想要录制电脑上的画面,这时候有人会想到下个第三方软件,其实可以用电脑上的自带...

    win10 系统自带录屏 详细教程

    + 更多教程 +
    ASP编程JSP编程PHP编程.NET编程python编程