1. 程式人生 > >【總結】清除webbrowser cookie/session的6種方法

【總結】清除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這個方法自己網上搜索一下.