Compartir a través de


CoreWebView2.WebResourceResponseReceived Event

Definition

WebResourceResponseReceived is raised when the WebView receives the response for a request for a web resource (any URI resolution performed by the WebView; such as HTTP/HTTPS, file and data requests from redirects, navigations, declarations in HTML, implicit Favicon lookups, and fetch API usage in the document).

public event EventHandler<Microsoft.Web.WebView2.Core.CoreWebView2WebResourceResponseReceivedEventArgs> WebResourceResponseReceived;
member this.WebResourceResponseReceived : EventHandler<Microsoft.Web.WebView2.Core.CoreWebView2WebResourceResponseReceivedEventArgs> 
Public Custom Event WebResourceResponseReceived As EventHandler(Of CoreWebView2WebResourceResponseReceivedEventArgs) 

Event Type

Examples

ShowNextWebResponse = false;

CoreWebView2WebResourceRequest request = e.Request;
CoreWebView2WebResourceResponseView response = e.Response;

string caption = "Web Resource Response Received";
// Start with capacity 64 for minimum message size
StringBuilder messageBuilder = new StringBuilder(64);
string HttpMessageContentToString(System.IO.Stream content) => content == null ? "[null]" : "[data]";
void AppendHeaders(IEnumerable headers)
{
    foreach (var header in headers)
    {
        messageBuilder.AppendLine($"  {header}");
    }
}

// Request
messageBuilder.AppendLine("Request");
messageBuilder.AppendLine($"URI: {request.Uri}");
messageBuilder.AppendLine($"Method: {request.Method}");
messageBuilder.AppendLine("Headers:");
AppendHeaders(request.Headers);
messageBuilder.AppendLine($"Content: {HttpMessageContentToString(request.Content)}");
messageBuilder.AppendLine();

// Response
messageBuilder.AppendLine("Response");
messageBuilder.AppendLine($"Status: {response.StatusCode}");
messageBuilder.AppendLine($"Reason: {response.ReasonPhrase}");
messageBuilder.AppendLine("Headers:");
AppendHeaders(response.Headers);
try
{
    Stream content = await response.GetContentAsync();
    messageBuilder.AppendLine($"Content: {HttpMessageContentToString(content)}");
}
catch (System.Runtime.InteropServices.COMException)
{
    messageBuilder.AppendLine($"Content: [failed to load]");
}

MessageBox.Show(messageBuilder.ToString(), caption);

Remarks

The host app can use this event to view the actual request and response for a web resource. There is no guarantee about the order in which the WebView processes the response and the host app's handler runs. The app's handler will not block the WebView from processing the response. The event args include the CoreWebView2WebResourceRequest as sent by the wire and CoreWebView2WebResourceResponse received, including any additional headers added by the network stack that were not be included as part of the associated WebResourceRequested event, such as Authentication headers.

Applies to