c#登陆qq空间无视JS加密

来源:互联网 发布:网络电视腾讯课堂 编辑:程序博客网 时间:2024/04/24 04:04

</pre><p>很早就有想法写出一个能无视加密的万能登陆的帮助类,本人也属于比较懒的类型一直没有写出来,如果是一般的网站模拟登陆还比较好处理,但是遇到淘宝或者qq空间之类你的网站时就比较难处理了。其实我们可以通过.Net给我们提供的WebBrowser空间来进行模拟点击从而获取登陆后的Cookie,这样子完全是真实的登陆,所以可以直接无视js加密。就在前几天心血来潮写了一个登陆QQ空间的程序,大家可以参考下,这里还没有做到通用目前还在写,大家如果有更好的想法也可以加入自己的想法,思路其实还是很简单的。Demo的下载地址:<a target=_blank href="http://www.studycsharp.com/forum.php?mod=viewthread&tid=17" target="_blank">C#登陆QQ空间</a></p><p></p><p><pre name="code" class="csharp">using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Forms;using System.Diagnostics;using System.Threading;using System.Runtime.InteropServices;namespace LoginUtil{    /// <summary>    /// 万能登录类    /// 使用的时候尽量开在子线程中,因为GetCookie方法在使用的过程中受网络影响会有堵塞。    /// </summary>    public class LoginUtils    {        [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]        private static extern bool InternetGetCookieEx(string pchURL, string pchCookieName, StringBuilder pchCookieData, ref uint pcchCookieData, int dwFlags, IntPtr lpReserved);        [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]        private static extern int InternetSetCookieEx(string lpszURL, string lpszCookieName, string lpszCookieData, int dwFlags, IntPtr dwReserved);        private static WebBrowser web = new WebBrowser();        private static bool LoginScuess = false;//是否登录成功        private static string ScuessTag2 = "";//登录成功标记        private static string Cookie = "";//登录成功的Cookie;        private static bool IsCompleted = false;//是否加载完成        private static Stopwatch watch = new Stopwatch();//计时器 计算超时间        static LoginUtils()        {            web.ScriptErrorsSuppressed = true;//取消js抱错弹窗,不然会一直提示js错误            web.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(web_DocumentCompleted);//注册WebBrowser加载页面完成的事件        }       /// <summary>        /// WebBrowser加载页面为按成事件       /// </summary>       /// <param name="sender"></param>       /// <param name="e"></param>       static  void web_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)        {            IsCompleted = true;            if (web.DocumentText.Contains(ScuessTag2))            {                LoginScuess = true;                Cookie = GetCookies(e.Url.ToString());//利用win32 api获取Cookie  这样子的不会出现Cookie获取不全的现象            }        }        /// <summary>        /// 万能登录方法        /// </summary>        /// <param name="UserNameDomID">用户名输入框的ID</param>        /// <param name="PassWordDomID">密码输入框的ID</param>        /// <param name="UserName">用户名</param>        /// <param name="PassWord">密码</param>        /// <param name="ScusessTag">成功标记</param>        /// <returns></returns>       public static string GetCookie(string LoginPageUrl, string UserNameDomID, string PassWordDomID, string LoginButtonDomID, string UserName, string PassWord, string ScuessTag)        {            try            {                IsCompleted = false;                LoginScuess = false;                ScuessTag2 = ScuessTag;                web.Navigate(LoginPageUrl);                while (web.ReadyState != WebBrowserReadyState.Complete)                {                    System.Windows.Forms.Application.DoEvents();                }                HtmlDocument htmlDoc = web.Document;                HtmlElement qqNumber = htmlDoc.GetElementById(UserNameDomID);                qqNumber.SetAttribute("value", UserName);                HtmlElement qqPassword = htmlDoc.GetElementById(PassWordDomID);                qqPassword.SetAttribute("value", PassWord);                HtmlElement btnLoginQzone = htmlDoc.GetElementById(LoginButtonDomID);                if (btnLoginQzone != null)                {                    btnLoginQzone.InvokeMember("click");                }            }            catch (Exception ex)            {                throw new Exception(ex.ToString());            }            watch.Start();//启动计时器            while (!LoginScuess)            {                System.Windows.Forms.Application.DoEvents();                if (watch.Elapsed.TotalSeconds >= 20) //10秒超时                {                    break;                }            }            return Cookie;        }       public static string GetCookies(string url)       {           uint pcchCookieData = 0x100;           StringBuilder pchCookieData = new StringBuilder((int)pcchCookieData);           if (!InternetGetCookieEx(url, null, pchCookieData, ref pcchCookieData, 0x2000, IntPtr.Zero))           {               if (pcchCookieData < 0)               {                   return null;               }               pchCookieData = new StringBuilder((int)pcchCookieData);               if (!InternetGetCookieEx(url, null, pchCookieData, ref pcchCookieData, 0x2000, IntPtr.Zero))               {                   return null;               }           }           return pchCookieData.ToString();       }    }}






0 0