/// <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; }
|
请发表评论