Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.
If you find these projects useful please consider becoming a sponsor with Patreon, GitHub or Liberapay.

how can I add cookie before browsing the url?

Post Reply
raychl
Posts: 4
Joined: Mon Nov 25, 2019 7:00 am

how can I add cookie before browsing the url?

Post by raychl »

I tried to add cookies at onnavigationstarting event, but it didn't work.
User avatar
salvadordf
Posts: 4563
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: how can I add cookie before browsing the url?

Post by salvadordf »

Hi,

This is what the API documentation say about AddOrUpdateCookie :
https://docs.microsoft.com/en-us/microsoft-edge/webview2/reference/win32/icorewebview2cookiemanager?view=webview2-1.0.1108.44#addorupdatecookie

Try adding your cookie with the CookieManager demo and then navigate to that site.
raychl
Posts: 4
Joined: Mon Nov 25, 2019 7:00 am

Re: how can I add cookie before browsing the url?

Post by raychl »

Thank you. It's working properly.

Add cookie OnNavigationStarting
....
TempCookie := WVBrowser1.CreateCookie('TestCookie', '1234', 'example.com', '/');
if assigned(TempCookie) then
try
WVBrowser1.AddOrUpdateCookie(TempCookie);
finally
TempCookie := nil;
end;
...
Get cookie OnNavigationCompleted
...
WVBrowser1.GetCookies;
...
Test cookie GetCookiesCompleted
...
var
ACookieSize:SYSUINT;
Acookie: ICoreWebView2Cookie;
AName,AValue,ADomain:PWideChar;
I:Integer;
begin
aCookieList.Get_Count(ACookieSize);
for I := 0 to ACookieSize-1 do
begin
aCookieList.GetValueAtIndex(i,Acookie);
Acookie.Get_name(AName);
Acookie.Get_value(AValue);
Acookie.Get_Domain(ADomain);
if(AName='TestCookie')then
begin
ShowMessage(AValue);
end;
end;
...
dvbss11
Posts: 28
Joined: Sat Oct 26, 2019 6:30 pm

how to set cookies to get Expires back

Post by dvbss11 »

how to set cookies to get Expires back in demo version uCookieVisitor.pas press SetCookies with Expires=now then want to see cookies but Expires = 12/31/1899
User avatar
salvadordf
Posts: 4563
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: how can I add cookie before browsing the url?

Post by salvadordf »

Hi,

I just uploaded a new WebView4Delphi version with a TCoreWebView2Cookie.ExpiresDate property.

That property reads and writes the same value as TCoreWebView2Cookie.Expires but in TDateTime format.

Set TCoreWebView2Cookie.ExpiresDate := Now; in the CookieManager demo and you will see the right date.
Post Reply