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.
Please try the code in the MiniBrowser demo :
https://github.com/salvadordf/WebView4Delphi/blob/226eab570d75d051f54eed36069c3e089d60dbe5/demos/Delphi_VCL/MiniBrowser/uMiniBrowser.pas#L1008
Using the wrapper classes ensures that the strings are handled correctly.
Make sure you get the headers for the right request because that event is triggered once for each resource request in the web page.
GetContent
Get the response content asynchronously.
public HRESULT GetContent(ICoreWebView2WebResourceResponseViewGetContentCompletedHandler * handler)
The handler will receive the response content stream.
This method returns null if content size is more than 123MB or for navigations that become downloads or if response is downloadable content type (e.g., application/octet-stream). See event to handle the response.add_DownloadStarting
But Why ICoreWebView2WebResourceResponseReceivedEventArgs.request.Get_uri(vU) is null ?
instead, ICefResponse<>nil in CEF.
We can Get the values of ICefResponse.MimeType, ICefResponse.GetHeaderByName and so on.
procedure TMiniBrowserFrm.WVBrowser1WebResourceResponseReceived(
Sender: TObject; const aWebView: ICoreWebView2;
const aArgs: ICoreWebView2WebResourceResponseReceivedEventArgs);
var
TempArgs : TCoreWebView2WebResourceResponseReceivedEventArgs;
TempResponse : TCoreWebView2WebResourceResponseView;
TempHeaders : TCoreWebView2HttpResponseHeaders;
TempIterator : TCoreWebView2HttpHeadersCollectionIterator;
TempRequest : TCoreWebView2WebResourceRequestRef;
TempName : wvstring;
TempValue : wvstring;
TempHandler : ICoreWebView2WebResourceResponseViewGetContentCompletedHandler;
begin
if FGetHeaders then
try
FHeaders.Clear;
FGetHeaders := False;
TempArgs := TCoreWebView2WebResourceResponseReceivedEventArgs.Create(aArgs);
TempResponse := TCoreWebView2WebResourceResponseView.Create(TempArgs.Response);
TempRequest := TCoreWebView2WebResourceRequestRef.Create(TempArgs.Request);
TempHandler := TCoreWebView2WebResourceResponseViewGetContentCompletedHandler.Create(WVBrowser1);
TempHeaders := TCoreWebView2HttpResponseHeaders.Create(TempResponse.Headers);
TempIterator := TCoreWebView2HttpHeadersCollectionIterator.Create(TempHeaders.Iterator);
FHeaders.Add('Request.URI: ' + TempRequest.URI);
// GetContent will trigger the TWVBrowserBase.OnWebResourceResponseViewGetContentCompleted
// event with the contents of this resource, which in this case corresponds to the HTML contents.
TempResponse.GetContent(TempHandler);
while TempIterator.HasCurrentHeader do
begin
if TempIterator.GetCurrentHeader(TempName, TempValue) then
FHeaders.Add(TempName + ': ' + TempValue);
TempIterator.MoveNext;
end;
finally
FreeAndNil(TempIterator);
FreeAndNil(TempHeaders);
FreeAndNil(TempResponse);
FreeAndNil(TempRequest);
FreeAndNil(TempArgs);
TempHandler := nil;
end;
end;
The request URI has the correct value. Please, use the class wrappers.
If you detect that WebView2 is not returning the request uri in some cases then report that issue to the feedback repository :
https://github.com/MicrosoftEdge/WebView2Feedback