Skip to content

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 file

Core Code Walkthrough

1. Declaration and Initialization (Form1.frm)

vb
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 Sub

This demo embeds WebView2 in a Frame control, demonstrating that Initialize can accept any window handle.

vb
Private Sub Command1_Click()
    Text1.Text = wv.Cookies.GetCookies
End Sub

Uses document.cookie parsing, does not include HttpOnly cookies. Returns a simplified format cookie string.

vb
Private Sub Command2_Click()
    Text1.Text = wv.Cookies.GetCookiesFull
End Sub

Uses CDP Network.getCookies retrieval, including HttpOnly cookies. Returns detailed cookie information in JSON format, including domain, path, httpOnly, secure, and other fields.

Feature Description

  1. Simple Cookie Mode

    • Retrieved via document.cookie
    • Returns human-readable cookie string
    • Does not include HttpOnly cookies (browser security restriction)
  2. Full Cookie Mode

    • Retrieved via CDP Network.getCookies
    • Includes HttpOnly cookies
    • Returns JSON format with complete cookie attributes
  3. Frame Control Embedding

    • WebView2 is not limited to Forms — can be embedded in any control with an hWnd
    • Uses Me.Frame1.hWnd to embed the browser in the Frame area

Technical Notes

  1. HttpOnly Cookies are cookies with the HttpOnly flag set; JavaScript's document.cookie cannot read them. Only CDP or the server side can access them
  2. The Cookies property is a read-only sub-object, accessed via wv.Cookies, not directly created
  3. GetCookiesFull returns JSON, which may require a JSON parsing library to extract specific fields

Use Cases

  1. Debugging web application cookie state
  2. Implementing auto-login (reading and setting cookies)
  3. Monitoring third-party cookies and privacy compliance

Extension Suggestions

  1. Use CDP's Network.setCookie and Network.deleteCookies for complete cookie management
  2. Combine with a timer to implement cookie monitoring for session expiration detection
  3. Use Cookies.GetCookiesFullAsync for asynchronous retrieval to avoid UI freezing

VB6 and LOGO copyright of Microsoft Corporation