【总结】清除webbrowser cookie/session的6种方法

下面是我测试下来的6种清除webbrowser中cookie的6种方法:

            //方法一:调用 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!");
            }

        }

技术分享

 

方法二:

就只有这一句就好了:

 //方法二:删除用户登录后的信息,这里相当于浏览器的注销功能,使用的是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,这个是从外国网站上看来的,实际无效!

方法六:

 //方法六:使用InternetSetCookie给cookie赋null值 (不推荐)
            //也可以给此Cookie赋空值:InternetSetCookie
            //InternetSetCookie("http://.qq.com/", NULL, "uin=; PATH=/; DOMAIN=qq.com");

 

关于InternetSetCookie这个方法自己网上搜索一下.


 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。