[英]How to delete Cookies from windows.form?
我正在用C#編寫的windows.form應用程序上使用Webbrowser控件。 我想編寫一種在Webbrowers控件訪問某個站點后從cookie中刪除cookie的方法。 不幸的是,我不知道該怎么做,也沒有在互聯網上找到很多幫助。
如果有人真的有這樣做的經驗,不僅是假設的,因為它可能比看起來要難,我不知道。
int count = webBrowser2.Document.Cookie.Length;
webBrowser2.Document.Cookie.Remove(0,count);
我只是假設像上面的代碼一樣可以工作,但我想不會。 任何人都可以對整個cookie事情有所了解嗎?
如果啟用了JavaScript,則可以使用此代碼段清除以清除Web瀏覽器當前所在站點的cookie。
webBrowser.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());}}}})())")
它源自此小書簽,用於清除cookie。
我從此處修改了解決方案: http : //mdb-blog.blogspot.ru/2013/02/c-winforms-webbrowser-clear-all-cookies.html
實際上,您不需要不安全的代碼。 這是對我有用的助手類:
public static class WinInetHelper
{
public static bool SupressCookiePersist()
{
// 3 = INTERNET_SUPPRESS_COOKIE_PERSIST
// 81 = INTERNET_OPTION_SUPPRESS_BEHAVIOR
return SetOption(81, 3);
}
public static bool EndBrowserSession()
{
// 42 = INTERNET_OPTION_END_BROWSER_SESSION
return SetOption(42, null);
}
static bool SetOption(int settingCode, int? option)
{
IntPtr optionPtr = IntPtr.Zero;
int size = 0;
if (option.HasValue)
{
size = sizeof (int);
optionPtr = Marshal.AllocCoTaskMem(size);
Marshal.WriteInt32(optionPtr, option.Value);
}
bool success = InternetSetOption(0, settingCode, optionPtr, size);
if (optionPtr != IntPtr.Zero) Marshal.Release(optionPtr);
return success;
}
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool InternetSetOption(
int hInternet,
int dwOption,
IntPtr lpBuffer,
int dwBufferLength
);
}
您可以在流程開始時的某個地方調用SupressCookiePersist,並在關閉瀏覽器時調用EndBrowserSession清除Cookie,如下所述: Facebook多帳戶
我找到了刪除所有cookie的解決方案。 在url上找到的示例,將在應用程序(進程)啟動時刪除cookie。
http://mdb-blog.blogspot.com/2013/02/c-winforms-webbrowser-clear-all-cookies.html
該解決方案使用InternetSetOption函數來宣布WEBBROWSER清除其所有內容。
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 !>?");
}
請注意,這僅按照MSDN INTERNET_OPTION_SUPPRESS_BEHAVIOR上的說明清除特定過程的cookie:
通用選項,用於在整個流程范圍內禁止行為。
嘗試使用此:
System.IO.File.Delete(Environment.SpecialFolder.Cookies.ToString() + "cookiename");
其他建議的答案的變體,不需要不安全的代碼或手動編組:
private static void SuppressCookiePersistence()
{
int flag = INTERNET_SUPPRESS_COOKIE_PERSIST;
if (!InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SUPPRESS_BEHAVIOR, ref flag, sizeof(int)))
{
var ex = Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());
throw ex;
}
}
const int INTERNET_OPTION_SUPPRESS_BEHAVIOR = 81;
const int INTERNET_SUPPRESS_COOKIE_PERSIST = 3;
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, ref int flag, int dwBufferLength);
webBrowser.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());}}}})())")
基於Internet Explorer的Web瀏覽器控件,因此當我們刪除IE cookie時,web瀏覽器cookie也被刪除。 因此,通過此答案,您可以嘗試以下操作:
System.Diagnostics.Process.Start("CMD.exe","/C RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2");
Web瀏覽器控件是否顯示頁面構成了您作為開發人員無法控制的多個站點,或者您只是使用Web瀏覽器控件來查看在應用程序中創建的自定義HTML頁面?
如果是前者,則cookie直接與設置它們的域綁定,因此要刪除這些cookie,您需要監視用戶的cookie目錄並刪除創建的任何新cookie,以跟蹤對現有cookie的更改。
如果是更高版本,則始終可以將webbrowser控件發送到自定義頁面,該頁面使用服務器端腳本(如果在應用程序中可用)或JavaScript刪除cookie。
我正在使用此代碼,並且沒有JavaScript就可以工作。 它的功能與JavaScript相同,只是在VB.NET中。 另外,無需導航。
For Each cookie As String In Document.Cookie.Split(";"c)
Dim domain As String = "." + url.Host
While domain.Length > 0
Dim path As String = url.LocalPath
While path.Length > 0
Document.Cookie = cookie.Split("="c)(0).Trim & "= ;expires=Thu, 30-Oct-1980 16:00:00 GMT;path=" & path & ";domain=" & domain
path = path.Substring(0, path.Length - 1)
End While
Select Case domain.IndexOf(".")
Case 0
domain = domain.Substring(1)
Case -1
domain = ""
Case Else
domain = domain.Substring(domain.IndexOf("."))
End Select
End While
Next
與JavaScript唯一的真正區別在於,我專門搜索=
並使cookie=
過期,而不是僅使cookie=value
過期。 這對於使沒有價值的Cookie過期非常重要。
陷阱:
ReadyState = WebBrowserReadyState.Complete
,不要訪問Document
,否則Document將為Nothing並且取消引用將引發異常。 帶有View Cookies加載項的 Firefox在調試特定網頁方面大有幫助。
經過一段很長的時間,發現如何在Webbrowser C#中銷毀會話,我使用代碼成功完成了:
webBrowser.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());}}}})())")
在上面這個話題上,喬丹·米爾恩怎么說?
在我的應用程序中,我需要使用webbrowser1.navigate(“ xxx”);
如果您使用C#屬性,則在這里不起作用。
我希望您能從中獲得有用的信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.