Training
Learning path
Use advance techniques in canvas apps to perform custom updates and optimization - Training
Use advance techniques in canvas apps to perform custom updates and optimization
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
The Microsoft Edge WebView2 control lets you interact with and modify network requests. You can either provide a response or modify the network request using the WebResourceRequested
and WebResourceResponseReceived
events. There is also special functionality that allows you to navigate with specific network requests using the NavigateWithWebResourceRequest API
.
This article describes how you can modify network requests. Use this API and approach to:
Terminology:
Term | Definition |
---|---|
intercept | Your host app can intercept a request that is sent from the WebView2 control to the HTTP server, read or modify the request, and then send the unchanged or modified request to the HTTP server (or to local code instead of the HTTP server). |
override | Your host app can override a response that's sent from the HTTP server to the WebView2 control, and send a custom response to the WebView2 control instead of the original response. |
The WebResourceRequested
event is a low-level API that gives more control, but requires more coding and is complicated to use. For some common scenarios, we provide APIs that are easier to use and are optimized for those specific scenarios, and we recommend you use those APIs rather than the APIs discussed in this article.
Instead of using the WebResourceRequested APIs, it's preferable to use these other approaches when feasible:
Note: For URLs with virtual hostnames, using the WebResourceRequested
event isn't supported. This is because the WebResourceRequested
event isn't fired for the SetVirtualHostNameToFolderMapping method.
The WebView2 control sits in between your host app and the HTTP server. When your host app navigates to a URI, the WebView2 control sends a request to the HTTP server. The HTTP server then sends a response to the WebView2 control.
Your host app can intercept a request that is sent from the WebView2 control to the HTTP server, read or modify the request, and then send the unchanged or modified request to the HTTP server (or to local code instead of the HTTP server).
Intercepting the request allows you to customize the header content, URL, or the GET/POST method. The host app may want to intercept a request to provide optional POST content as part of the request.
The host app can change the properties of a request by using this API:
A HTTP header provides important information and metadata about a request or response. Changing headers enables you to perform powerful actions on the network.
A request header can be used to indicate the format of the response (such as the Accept-*
headers), set authentication tokens, read and write cookies (sensitive information), modify the user agent, and so on. A response header can be used to provide more context of the response.
In order to receive WebResourceRequested
events, specify filters for the requests that the host app is interested in, based on URL and resource type.
For example, suppose the host app is trying to replace images. In this case, the host app is only interested in WebResourceRequested
events for images. The host app would only get events for images by specifying the resourceContext
filter for images.
Another example is if the host app is only interested in all requests that are under a site like https://example.com
. Then the app can specify a URL filter as https://example.com/*
to get events that are associated with that site.
For details about how the URL filter works, see CoreWebView2.AddWebResourceRequestedFilter Method > Remarks
Intercepting requests sent from WebView2 enables you to further configure your request. The host app might want to provide optional content as part of the request that the WebView2 control won't know on its own. Some scenarios include:
WebResourceRequested
filter.WebResourceRequested
and WebResourceResponseReceived
.WebResourceRequested
event to the host app.WebResourceRequested
event.WebResourceRequested
event, which means that the host app asks for more time to decide what to do.WebResourceResponseReceived
event.WebResourceResponseReceived
event and handles it.In the following example, the host app intercepts the document request that is sent from the WebView2 control to the http://www.example.com
HTTP server, adds a custom header value and sends the request.
// Add a filter to select all resource types under http://www.example.com
webView.CoreWebView2.AddWebResourceRequestedFilter(
"http://www.example.com/*", CoreWebView2WebResourceContext.All);
webView.CoreWebView2.WebResourceRequested += delegate (
object sender, CoreWebView2WebResourceRequestedEventArgs args) {
CoreWebView2WebResourceContext resourceContext = args.ResourceContext;
// Only intercept the document resources
if (resourceContext != CoreWebView2WebResourceContext.Document)
{
return;
}
CoreWebView2HttpRequestHeaders requestHeaders = args.Request.Headers;
requestHeaders.SetHeader("Custom", "Value");
};
By default, the HTTP server sends responses to the WebView2 control. Your host app can override a response that's sent from the HTTP server to the WebView2 control, and send a custom response to the WebView2 control instead of the original response.
WebResourceRequested
filter.WebResourceRequested
and WebResourceResponseReceived
.WebResourceRequested
event to the host app.WebResourceRequested
event.WebResourceRequested
event handler. The host app can also defer the WebResourceRequested
event, which means that the host app asks for more time to decide what to do.// Add a filter to select all image resources
webView.CoreWebView2.AddWebResourceRequestedFilter(
"*", CoreWebView2WebResourceContext.Image);
webView.CoreWebView2.WebResourceRequested += delegate (
object sender, CoreWebView2WebResourceRequestedEventArgs args) {
// Replace the remote image resource with a local one specified at the path customImagePath.
// If response is not set, the request will continue as it is.
FileStream fs = File.Open(customImagePath, FileMode.Open);
CoreWebView2WebResourceResponse response = webView.CoreWebView2.Environment.CreateWebResourceResponse(fs, 200, "OK", "Content-Type: image/jpeg");
args.Response = response;
};
The NavigateWithWebResourceRequest
method allows your host app to navigate the WebView2 control using a custom WebResourceRequest
. You can use this API to create a GET or POST request that has custom headers and content. Then the WebView2 control will navigate by using this custom request.
// This code posts text input=Hello to the POST form page in W3Schools.
// Need to convert post data to UTF-8 as required by the application/x-www-form-urlencoded Content-Type
UTF8Encoding utfEncoding = new UTF8Encoding();
byte[] postData = utfEncoding.GetBytes("input=Hello");
MemoryStream postDataStream = new MemoryStream(postData.Length);
postDataStream.Write(postData, 0, postData.Length);
postDataStream.Seek(0, SeekOrigin.Begin);
// This acts as a HTML form submit to https://www.w3schools.com/action_page.php
CoreWebView2WebResourceRequest webResourceRequest =
environment.CreateWebResourceRequest("https://www.w3schools.com/action_page.php",
"POST",
postDataStream,
"Content-Type: application/x-www-form-urlencoded");
webView.CoreWebView2.NavigateWithWebResourceRequest(webResourceRequest);
You can monitor the requests and responses via the WebResourceResponseReceived
event, to read any header value.
This example shows how to read the authorization header value by monitoring the requests and responses via the WebResourceResponseReceived
event.
The following code demonstrates how the WebResourceResponseReceived
event can be used.
WebView.CoreWebView2.WebResourceResponseReceived += CoreWebView2_WebResourceResponseReceived;
// Note: modifications made to request are set but have no effect on WebView processing it.
private async void WebView_WebResourceResponseReceived(object sender, CoreWebView2WebResourceResponseReceivedEventArgs e)
{
// Actual headers sent with request
foreach (var current in e.Request.Headers)
{
Console.WriteLine(current);
}
// Headers in response received
foreach (var current in e.Response.Headers)
{
Console.WriteLine(current);
}
// Status code from response received
int status = e.Response.StatusCode;
if (status == 200)
{
Console.WriteLine("Request succeeded!");
// Get response body
try
{
System.IO.Stream content = await e.Response.GetContentAsync();
// Null will be returned if no content was found for the response.
if (content != null)
{
DoSomethingWithResponseContent(content);
}
}
catch (COMException ex)
{
// A COMException will be thrown if the content failed to load.
}
}
}
Request:
Content
Headers
Method
Uri
Request
ResourceContext
Response
GetDeferral
Response:
Content
Headers
ReasonPhrase
StatusCode
Request
Response
Headers
ReasonPhrase
StatusCode
GetContentAsync
Training
Learning path
Use advance techniques in canvas apps to perform custom updates and optimization - Training
Use advance techniques in canvas apps to perform custom updates and optimization