I'm using this code and it works without JavaScript. It's doing the same things as the JavaScript, but in VB.NET. Also, no navigation needed.
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
The only real difference from the JavaScript is where, instead of just expiring cookie=value
, I specifically search for the =
and expire cookie=
. This is important for expiring cookies that have no value.
Pitfalls:
- You can only delete the cookies of the website to which you have navigated.
- If the page is redirected to a different domain, the cookies you remove might be from the redirected domain. Or not, it's a race between the redirect and your code.
- Don't access
Document
until ReadyState = WebBrowserReadyState.Complete
, otherwise Document will be Nothing and the dereference will throw an exception.
- Probably lots of others, but this code works great for me.
Firefox with the View Cookies Add-On helped a lot in debugging specific web pages.