• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

微信公众平台主动推送消息(asp.net)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

/// <summary>
        /// MD5 32位加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        static string GetMd5Str32(string str)
        {
            MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
            // Convert the input string to a byte array and compute the hash. 
            char[] temp = str.ToCharArray();
            byte[] buf = new byte[temp.Length];
            for (int i = 0; i < temp.Length; i++)
            {
                buf[i] = (byte)temp[i];
            }
            byte[] data = md5Hasher.ComputeHash(buf);
            // Create a new Stringbuilder to collect the bytes 
            // and create a string. 
            StringBuilder sBuilder = new StringBuilder();
            // Loop through each byte of the hashed data  
            // and format each one as a hexadecimal string. 
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            // Return the hexadecimal string. 
            return sBuilder.ToString();
        }
 
        public static bool ExecLogin()
        {
            bool result = false;
            string password = GetMd5Str32(strMPPassword).ToUpper(); //"D0DCBF0D12A6B1E7FBFA2CE5848F3EFF";
            string padata = "username=" + System.Web.HttpUtility.UrlEncode(strMPAccount) + "&pwd=" + password + "&imgcode=&f=json";
            string url = "http://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN ";//请求登录的URL
            try
            {
                CookieContainer cc = new CookieContainer();//接收缓存
                byte[] byteArray = Encoding.UTF8.GetBytes(padata); // 转化
                HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(url);
                webRequest2.CookieContainer = cc;
                webRequest2.Method = "POST";
                webRequest2.ContentType = "application/x-www-form-urlencoded";
                webRequest2.ContentLength = byteArray.Length;
                Stream newStream = webRequest2.GetRequestStream();
                // Send the data.
                newStream.Write(byteArray, 0, byteArray.Length);    //写入参数
                newStream.Close();
                HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();
                StreamReader sr2 = new StreamReader(response2.GetResponseStream(), Encoding.Default);
                string text2 = sr2.ReadToEnd();
 
                //此处用到了newtonsoft来序列化
                SunnyInfo.Web.Class.WeiXinRetInfo retinfo = Newtonsoft.Json.JsonConvert.DeserializeObject<SunnyInfo.Web.Class.WeiXinRetInfo>(text2);
                string token = string.Empty;
                if (retinfo.ErrMsg.Length > 0)
                {
                    token = retinfo.ErrMsg.Split(new char[] { '&' })[2].Split(new char[] { '=' })[1].ToString();//取得令牌
                    LoginInfo.LoginCookie = cc;
                    LoginInfo.CreateDate = DateTime.Now;
                    LoginInfo.Token = token;
                    result = true;
                }
            }
            catch (Exception ex)
            {
                Company.PubClass.WriteLog("Erro[" + DateTime.Now.ToString() + "]" + ex.StackTrace);
            }
            return result;
        }
 
        public static class LoginInfo
        {
            /// <summary>
            /// 登录后得到的令牌
            /// </summary>       
            public static string Token { get; set; }
            /// <summary>
            /// 登录后得到的cookie
            /// </summary>
            public static CookieContainer LoginCookie { get; set; }
            /// <summary>
            /// 创建时间
            /// </summary>
            public static DateTime CreateDate { get; set; }
 
        }
 
//该代码有上海曦熙信息科技有限公司整理
        public static bool SendMessage(string Message, string fakeid)
        {
            bool result = false;
            CookieContainer cookie = null;
            string token = null;
 
            //此处的作用是判断Cookie是否过期如果过期就重新获取,获取cookie的方法本人在.net 实现微信公众平台的主动推送信息中有源码。
            if (null == Class.WeiXinLogin.LoginInfo.LoginCookie || Class.WeiXinLogin.LoginInfo.CreateDate.AddMinutes(Convert.ToInt32(Class.WeiXinLogin.strLoingMinutes)) < DateTime.Now)
            {
                Class.WeiXinLogin.ExecLogin();
            }
            cookie = Class.WeiXinLogin.LoginInfo.LoginCookie;//取得cookie
            token = Class.WeiXinLogin.LoginInfo.Token;//取得token
 
            string strMsg = System.Web.HttpUtility.UrlEncode(Message);
            string padate = "type=1&content=" + strMsg + "&error=false&tofake))
            {
                result = true;
            }
            return result;
        }


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ASP.NETMVCWEBAPIOAuth2Authorize发布时间:2022-07-10
下一篇:
asp.net连接数据库字符串发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap