【總結】清除webbrowser cookie/session的6種方法

下面是我測試下來的6種清除webbrowser中cookie的6種方法:javascript

複製代碼
            //方法一:調用 wininet.dll清除cookie (推薦)
            SuppressWininetBehavior();

            //方法二:刪除用戶登陸後的信息,這裏至關於瀏覽器的註銷功能,使用的是ie自帶的功能 (推薦)
            HtmlDocument document = wb.Document;
            document.ExecCommand("ClearAuthenticationCache", false, null);

            //方法三:刪除本機cookie 此方法會彈出ie清除cookie的彈出框
            //Temporary Internet Files  (Internet臨時文件)
            //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8
            //Cookies
            //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2
            //History (歷史記錄)
            //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 1
            //Form. Data (表單數據)
            //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 16
            //Passwords (密碼)
            //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 32
            //Delete All  (所有刪除)
            //ShellExecute(IntPtr.Zero, "open", "rundll32.exe", " InetCpl.cpl,ClearMyTracksByProcess 2", "", ShowCommands.SW_HIDE);
            ShellExecute(IntPtr.Zero, "open", "rundll32.exe", " InetCpl.cpl,ClearMyTracksByProcess 255", "", ShowCommands.SW_HIDE);


            //方法四:使用webbrowser自帶的清coookie的方法 (不推薦,清不掉session,實測無效)
            wb.Document.Cookie.Remove(0, (wb.Document.Cookie.Count() - 1));

            //方法五:使用js清除cookie (不推薦,清不掉session)
            wb.Navigate("javascript:void((function(){var a,b,c,e,f;f=0;a=document.cookie.split('; ');for(e=0;e<a.length&&a[e];e++){f++;for(b='.'+location.host;b;b=b.replace(/^(?:%5C.|[^%5C.]+)/,'')){for(c=location.pathname;c;c=c.replace(/.$/,'')){document.cookie=(a[e]+'; domain='+b+'; path='+c+'; expires='+new Date((new Date()).getTime()-1e11).toGMTString());}}}})())");
            //var a,b,c,e,f;
            //f=0;
            //a=document.cookie.split('; ');
            //b='.'+'baidu.com';
            ////b='.'+'www.baidu.com';
            //for(e=0;e<a.length;e++){
            //    //b='.'+location.host;
            //    b=b.replace(/^(?:%5C.|[^%5C.]+)/,'');
            //    c=location.pathname;
            //    c=c.replace(/.$/,'');
            //    ck = a[e]+'; domain='+b+'; path='+c+'; expires='+new Date((new Date()).getTime()-1e11).toGMTString();
            //    console.log(ck);
            //    document.cookie=ck;
            //}

            //方法六:使用InternetSetCookie給cookie賦null值 (不推薦)
            //也能夠給此Cookie賦空值:InternetSetCookie
            //InternetSetCookie("http://.qq.com/", NULL, "uin=; PATH=/; DOMAIN=qq.com");
複製代碼

 

方法一:

複製代碼
    [System.Runtime.InteropServices.DllImport("wininet.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
        public static extern bool InternetSetOption(int hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);


        /// <summary>
        /// 使用InternetSetOption操做wininet.dll清除webbrowser裏的cookie
        /// </summary>
        private static unsafe void SuppressWininetBehavior()
        {
            /* SOURCE: http://msdn.microsoft.com/en-us/library/windows/desktop/aa385328%28v=vs.85%29.aspx
                * INTERNET_OPTION_SUPPRESS_BEHAVIOR (81):
                *      A general purpose option that is used to suppress behaviors on a process-wide basis. 
                *      The lpBuffer parameter of the function must be a pointer to a DWORD containing the specific behavior to suppress. 
                *      This option cannot be queried with InternetQueryOption. 
                *      
                * INTERNET_SUPPRESS_COOKIE_PERSIST (3):
                *      Suppresses the persistence of cookies, even if the server has specified them as persistent.
                *      Version:  Requires Internet Explorer 8.0 or later.
                */
            int option = (int)3/* INTERNET_SUPPRESS_COOKIE_PERSIST*/;
            int* optionPtr = &option;

            bool success = InternetSetOption(0, 81/*INTERNET_OPTION_SUPPRESS_BEHAVIOR*/, new IntPtr(optionPtr), sizeof(int));
            if (!success)
            {
                MessageBox.Show("Something went wrong ! Clear Cookie Failed!");
            }

        }
複製代碼

 

方法二:

就只有這一句就行了:java

 //方法二:刪除用戶登陸後的信息,這裏至關於瀏覽器的註銷功能,使用的是ie自帶的功能 (推薦)
            HtmlDocument document = wb.Document;
            document.ExecCommand("ClearAuthenticationCache", false, null);

方法三:

複製代碼
 //方法三:刪除本機cookie 此方法會彈出ie清除cookie的彈出框
            //Temporary Internet Files  (Internet臨時文件)
            //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8
            //Cookies
            //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2
            //History (歷史記錄)
            //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 1
            //Form. Data (表單數據)
            //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 16
            //Passwords (密碼)
            //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 32
            //Delete All  (所有刪除)
            //ShellExecute(IntPtr.Zero, "open", "rundll32.exe", " InetCpl.cpl,ClearMyTracksByProcess 2", "", ShowCommands.SW_HIDE);
            ShellExecute(IntPtr.Zero, "open", "rundll32.exe", " InetCpl.cpl,ClearMyTracksByProcess 255", "", ShowCommands.SW_HIDE);
複製代碼
ShellExecute方法:
複製代碼
    public enum ShowCommands : int
        {

            SW_HIDE = 0,

            SW_SHOWNORMAL = 1,

            SW_NORMAL = 1,

            SW_SHOWMINIMIZED = 2,

            SW_SHOWMAXIMIZED = 3,

            SW_MAXIMIZE = 3,

            SW_SHOWNOACTIVATE = 4,

            SW_SHOW = 5,

            SW_MINIMIZE = 6,

            SW_SHOWMINNOACTIVE = 7,

            SW_SHOWNA = 8,

            SW_RESTORE = 9,

            SW_SHOWDEFAULT = 10,

            SW_FORCEMINIMIZE = 11,

            SW_MAX = 11

        }

        [DllImport("shell32.dll")]
        static extern IntPtr ShellExecute(IntPtr hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, ShowCommands nShowCmd);
複製代碼

 

方法四:

  //方法四:使用webbrowser自帶的清coookie的方法 (不推薦,清不掉session,實測無效)
            wb.Document.Cookie.Remove(0, (wb.Document.Cookie.Count() - 1));

方法五:

複製代碼
 //方法五:使用js清除cookie (不推薦,清不掉session)
            wb.Navigate("javascript:void((function(){var a,b,c,e,f;f=0;a=document.cookie.split('; ');for(e=0;e<a.length&&a[e];e++){f++;for(b='.'+location.host;b;b=b.replace(/^(?:%5C.|[^%5C.]+)/,'')){for(c=location.pathname;c;c=c.replace(/.$/,'')){document.cookie=(a[e]+'; domain='+b+'; path='+c+'; expires='+new Date((new Date()).getTime()-1e11).toGMTString());}}}})())");
            //var a,b,c,e,f;
            //f=0;
            //a=document.cookie.split('; ');
            //b='.'+'baidu.com';
            ////b='.'+'www.baidu.com';
            //for(e=0;e<a.length;e++){
            //    //b='.'+location.host;
            //    b=b.replace(/^(?:%5C.|[^%5C.]+)/,'');
            //    c=location.pathname;
            //    c=c.replace(/.$/,'');
            //    ck = a[e]+'; domain='+b+'; path='+c+'; expires='+new Date((new Date()).getTime()-1e11).toGMTString();
            //    console.log(ck);
            //    document.cookie=ck;
            //}
複製代碼

將 wb.Navigate("javascript:void((function(){。。。}裏的內容換成下面註釋掉的代碼,寫好你要清cookier 的domain而後就能夠清了,但清不掉session,這個是從外國網站上看來的,實際無效!web

方法六:

 //方法六:使用InternetSetCookie給cookie賦null值 (不推薦)
            //也能夠給此Cookie賦空值:InternetSetCookie
            //InternetSetCookie("http://.qq.com/", NULL, "uin=; PATH=/; DOMAIN=qq.com");

 

關於InternetSetCookie這個方法本身網上搜索一下.shell

相關文章
相關標籤/搜索