WebView2 Cookies Demo - Cookie Access
Overview
Demonstrates cWebView2Host's cookie management functionality, including two retrieval methods: simplified mode (document.cookie) and full mode (CDP Network.getCookies, including HttpOnly cookies).
Project Structure
Download Source [ Note: Re-register the DLL file in the bin directory ]
Source code is available in the VBMAN2 project's demos/webview2/cookies directory.
cookies/
├── Form1.frm # Main form, containing cookie retrieval logic
├── Form1.frx # Form layout binary data
├── vbman2_webview2.vbp # VB6 project file
└── vbman2_webview2.vbw # VB6 workspace fileCore Code Walkthrough
1. Declaration and Initialization (Form1.frm)
Dim wv As New cWebView2Host
Private Sub Form_Load()
' Note: WebView2 is embedded in a Frame control, not the entire Form
wv.Initialize Me.Frame1.hWnd, "https://vb6.pro"
End SubThis demo embeds WebView2 in a Frame control, demonstrating that Initialize can accept any window handle.
2. Simple Cookie Retrieval
Private Sub Command1_Click()
Text1.Text = wv.Cookies.GetCookies
End SubUses document.cookie parsing, does not include HttpOnly cookies. Returns a simplified format cookie string.
3. Full Cookie Retrieval
Private Sub Command2_Click()
Text1.Text = wv.Cookies.GetCookiesFull
End SubUses CDP Network.getCookies retrieval, including HttpOnly cookies. Returns detailed cookie information in JSON format, including domain, path, httpOnly, secure, and other fields.
Feature Description
Simple Cookie Mode
- Retrieved via
document.cookie - Returns human-readable cookie string
- Does not include HttpOnly cookies (browser security restriction)
- Retrieved via
Full Cookie Mode
- Retrieved via CDP
Network.getCookies - Includes HttpOnly cookies
- Returns JSON format with complete cookie attributes
- Retrieved via CDP
Frame Control Embedding
- WebView2 is not limited to Forms — can be embedded in any control with an hWnd
- Uses
Me.Frame1.hWndto embed the browser in the Frame area
Technical Notes
- HttpOnly Cookies are cookies with the HttpOnly flag set; JavaScript's
document.cookiecannot read them. Only CDP or the server side can access them - The Cookies property is a read-only sub-object, accessed via
wv.Cookies, not directly created - GetCookiesFull returns JSON, which may require a JSON parsing library to extract specific fields
Use Cases
- Debugging web application cookie state
- Implementing auto-login (reading and setting cookies)
- Monitoring third-party cookies and privacy compliance
Extension Suggestions
- Use CDP's
Network.setCookieandNetwork.deleteCookiesfor complete cookie management - Combine with a timer to implement cookie monitoring for session expiration detection
- Use
Cookies.GetCookiesFullAsyncfor asynchronous retrieval to avoid UI freezing