You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: microsoft-edge/webview2/how-to/webresourcerequested.md
+43-33Lines changed: 43 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ ms.author: msedgedevrel
6
6
ms.topic: conceptual
7
7
ms.prod: microsoft-edge
8
8
ms.technology: webview
9
-
ms.date: 04/29/2022
9
+
ms.date: 08/03/2023
10
10
---
11
11
# Custom management of network requests
12
12
<!--
@@ -49,7 +49,8 @@ Instead of using the WebResourceRequested APIs, it's preferable to use these oth
49
49
**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](/microsoft-edge/webview2/reference/win32/icorewebview2_3#setvirtualhostnametofoldermapping).<!-- or ClearVirtualHostNameToFolderMapping. -->
50
50
51
51
52
-
### How your host app, the WebView2 control, and the HTTP server interact
52
+
<!-- ------------------------------ -->
53
+
#### How your host app, the WebView2 control, and the HTTP server interact
53
54
54
55
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.
55
56
@@ -69,35 +70,39 @@ Intercepting the request allows you to customize the header content, URL, or the
69
70
70
71
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](https://developer.mozilla.org/docs/Glossary/HTTP_header) enables you to perform powerful actions on the network.
85
88
86
89
A [request header](https://developer.mozilla.org/docs/Glossary/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](https://developer.mozilla.org/docs/Glossary/Response_header) can be used to provide more context of the response.
87
90
88
-
### Filtering the WebResourceRequested event based on URL and resource type
91
+
92
+
<!-- ------------------------------ -->
93
+
#### Filtering the WebResourceRequested event based on URL and resource type
89
94
90
95
In order to receive `WebResourceRequested` events, specify filters for the requests that the host app is interested in, based on URL and resource type.
91
96
92
97
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.
93
98
94
99
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.
@@ -107,15 +112,17 @@ Another example is if the host app is only interested in all requests that are u
107
112
For details about how the URL filter works, see [CoreWebView2.AddWebResourceRequestedFilter Method > Remarks](/dotnet/api/microsoft.web.webview2.core.corewebview2.addwebresourcerequestedfilter#remarks)
108
113
109
114
110
-
### Why would you want to intercept requests that are sent from WebView2?
115
+
<!-- ------------------------------ -->
116
+
#### Why would you want to intercept requests that are sent from WebView2?
111
117
112
118
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:
113
119
* You're logging into a page and the app has credentials so the app can provide authentication header without the user having to enter those credentials.
114
120
* You want offline functionality in the app so you redirect the URL to a local file path when no internet connection is detected.
115
121
* You want to upload local file content to the request server via a POST request.
116
122
117
123
118
-
### Sequence for modifying requests
124
+
<!-- ------------------------------ -->
125
+
#### Sequence for modifying requests
119
126
120
127
<!-- wiki page that points to the Visio source file: Documentation > "Notes about specific image files" -->
121
128

@@ -134,16 +141,15 @@ Intercepting requests sent from WebView2 enables you to further configure your r
134
141
1. The host app listens for the `WebResourceResponseReceived` event and handles it.<!-- todo: arrow: "The WebView2 control creates a request for a resource that's needed for the webpage." -->
### Example: Intercepting a request, to monitor or modify it
139
-
<!-- ## Example: Header modification when making a request -->
144
+
<!-- ------------------------------ -->
145
+
#### Example: Intercepting a request, to monitor or modify it
140
146
141
147
<!-- this example doesn't exist in the sample repo -->
142
148
143
-
<!--note: the below intro is based on copying the main h2's Sentence 1 from above: -->
149
+
<!-- the below intro is based on copying the main h2's Sentence 1 from above: -->
144
150
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.
145
151
146
-
# [.NET](#tab/dotnet)
152
+
#####[.NET](#tab/dotnet)
147
153
148
154
```csharp
149
155
// Add a filter to select all resource types under http://www.example.com
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.
204
211
205
212
206
-
### Sequence for overriding responses
213
+
<!-- ------------------------------ -->
214
+
#### Sequence for overriding responses
207
215
208
216
<!-- wiki page that points to the Visio source file: Documentation > "Notes about specific image files" -->
209
217

@@ -219,9 +227,10 @@ By default, the HTTP server sends responses to the WebView2 control. Your host
219
227
<!-- todo: remove "then" from diagram step 8 -->
220
228
221
229
222
-
### Example: Overriding a response, to proactively replace it
230
+
<!-- ------------------------------ -->
231
+
#### Example: Overriding a response, to proactively replace it
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.
0 commit comments