Skip to content

Commit 79734ba

Browse files
authored
Merge pull request #2756 from MicrosoftDocs/user/mikehoffms/monitor-requests
Fixed code for WebResourceResponseReceived event
2 parents dca57b9 + 19b42a6 commit 79734ba

1 file changed

Lines changed: 43 additions & 33 deletions

File tree

microsoft-edge/webview2/how-to/webresourcerequested.md

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ms.author: msedgedevrel
66
ms.topic: conceptual
77
ms.prod: microsoft-edge
88
ms.technology: webview
9-
ms.date: 04/29/2022
9+
ms.date: 08/03/2023
1010
---
1111
# Custom management of network requests
1212
<!--
@@ -49,7 +49,8 @@ Instead of using the WebResourceRequested APIs, it's preferable to use these oth
4949
**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. -->
5050

5151

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
5354

5455
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.
5556

@@ -69,35 +70,39 @@ Intercepting the request allows you to customize the header content, URL, or the
6970

7071
The host app can change the properties of a request by using this API:
7172

72-
# [.NET](#tab/dotnet)
73+
##### [.NET](#tab/dotnet)
7374

7475
* [CoreWebView2WebResourceRequest Class](/dotnet/api/microsoft.web.webview2.core.corewebview2webresourcerequest)
7576

76-
# [Win32](#tab/win32)
77+
##### [Win32](#tab/win32)
7778

7879
* [ICoreWebView2WebResourceRequest](/microsoft-edge/webview2/reference/win32/icorewebview2webresourcerequest)
7980

8081
---
8182

82-
### What you can do with headers
83+
84+
<!-- ------------------------------ -->
85+
#### What you can do with headers
8386

8487
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.
8588

8689
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.
8790

88-
### Filtering the WebResourceRequested event based on URL and resource type
91+
92+
<!-- ------------------------------ -->
93+
#### Filtering the WebResourceRequested event based on URL and resource type
8994

9095
In order to receive `WebResourceRequested` events, specify filters for the requests that the host app is interested in, based on URL and resource type.
9196

9297
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.
9398

9499
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.
95100

96-
# [.NET](#tab/dotnet)
101+
##### [.NET](#tab/dotnet)
97102

98103
* [CoreWebView2.AddWebResourceRequestedFilter Method](/dotnet/api/microsoft.web.webview2.core.corewebview2.addwebresourcerequestedfilter)
99104

100-
# [Win32](#tab/win32)
105+
##### [Win32](#tab/win32)
101106

102107
* [AddWebResourceRequestedFilter method](/microsoft-edge/webview2/reference/win32/icorewebview2#addwebresourcerequestedfilter)
103108

@@ -107,15 +112,17 @@ Another example is if the host app is only interested in all requests that are u
107112
For details about how the URL filter works, see [CoreWebView2.AddWebResourceRequestedFilter Method > Remarks](/dotnet/api/microsoft.web.webview2.core.corewebview2.addwebresourcerequestedfilter#remarks)
108113

109114

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?
111117

112118
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:
113119
* 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.
114120
* You want offline functionality in the app so you redirect the URL to a local file path when no internet connection is detected.
115121
* You want to upload local file content to the request server via a POST request.
116122

117123

118-
### Sequence for modifying requests
124+
<!-- ------------------------------ -->
125+
#### Sequence for modifying requests
119126

120127
<!-- wiki page that points to the Visio source file: Documentation > "Notes about specific image files" -->
121128
![Diagram of sequence for modifying requests](webresourcerequested-images/sequence-for-modifying-requests.png)
@@ -134,16 +141,15 @@ Intercepting requests sent from WebView2 enables you to further configure your r
134141
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." -->
135142

136143

137-
<!-- ====================================================================== -->
138-
### 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
140146

141147
<!-- this example doesn't exist in the sample repo -->
142148

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: -->
144150
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.
145151

146-
# [.NET](#tab/dotnet)
152+
##### [.NET](#tab/dotnet)
147153

148154
```csharp
149155
// Add a filter to select all resource types under http://www.example.com
@@ -162,7 +168,7 @@ webView.CoreWebView2.WebResourceRequested += delegate (
162168
}
163169
```
164170

165-
# [Win32](#tab/win32)
171+
##### [Win32](#tab/win32)
166172

167173
```cpp
168174
// Add a filter to select all resource types under http://www.example.com
@@ -195,6 +201,7 @@ m_webView->add_WebResourceRequested(
195201

196202
---
197203

204+
198205
<!-- ====================================================================== -->
199206
## Overriding a response, to proactively replace it
200207
<!-- Overriding and providing new responses to WebView2 -->
@@ -203,7 +210,8 @@ m_webView->add_WebResourceRequested(
203210
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.
204211

205212

206-
### Sequence for overriding responses
213+
<!-- ------------------------------ -->
214+
#### Sequence for overriding responses
207215

208216
<!-- wiki page that points to the Visio source file: Documentation > "Notes about specific image files" -->
209217
![Diagram of sequence for overriding responses](webresourcerequested-images/sequence-for-overriding-responses.png)
@@ -219,9 +227,10 @@ By default, the HTTP server sends responses to the WebView2 control. Your host
219227
<!-- todo: remove "then" from diagram step 8 -->
220228

221229

222-
### Example: Overriding a response, to proactively replace it
230+
<!-- ------------------------------ -->
231+
#### Example: Overriding a response, to proactively replace it
223232

224-
# [.NET](#tab/dotnet)
233+
##### [.NET](#tab/dotnet)
225234

226235
```csharp
227236
// Add a filter to select all image resources
@@ -238,7 +247,7 @@ webView.CoreWebView2.WebResourceRequested += delegate (
238247
};
239248
```
240249

241-
# [Win32](#tab/win32)
250+
##### [Win32](#tab/win32)
242251

243252
```cpp
244253
// Add a filter to select all image resources
@@ -283,18 +292,19 @@ m_webView->add_WebResourceRequested(
283292
284293
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.
285294
286-
# [.NET](#tab/dotnet)
295+
##### [.NET](#tab/dotnet)
287296
288297
* [CoreWebView2.NavigateWithWebResourceRequest(CoreWebView2WebResourceRequest) Method](/dotnet/api/microsoft.web.webview2.core.corewebview2.navigatewithwebresourcerequest)
289298
290-
# [Win32](#tab/win32)
299+
##### [Win32](#tab/win32)
291300
292301
* [interface ICoreWebView2_2::NavigateWithWebResourceRequest method](/microsoft-edge/webview2/reference/win32/icorewebview2_2#navigatewithwebresourcerequest)
293302
294303
---
295304
296-
<!-- ====================================================================== -->
297-
### Example: Constructing a custom request and navigating using that request
305+
306+
<!-- ------------------------------ -->
307+
#### Example: Constructing a custom request and navigating using that request
298308
299309
<!-- This is an existing example in the Win32 sample app. -->
300310
@@ -305,8 +315,7 @@ from https://github.com/MicrosoftEdge/WebView2Feedback/blob/main/specs/NavigateW
305315
306316
307317
<!-- -------------------------------------------------- -->
308-
309-
# [.NET](#tab/dotnet)
318+
##### [.NET](#tab/dotnet)
310319
311320
```csharp
312321
// This code posts text input=Hello to the POST form page in W3Schools.
@@ -330,7 +339,7 @@ webView.CoreWebView2.NavigateWithWebResourceRequest(webResourceRequest);
330339

331340
<!-- -------------------------------------------------- -->
332341

333-
# [Win32](#tab/win32)
342+
##### [Win32](#tab/win32)
334343

335344
```cpp
336345
// This code posts text input=Hello to the POST form page in W3Schools.
@@ -366,7 +375,8 @@ webview->NavigateWithWebResourceRequest(webResourceRequest.get());
366375
You can monitor the requests and responses via the `WebResourceResponseReceived` event, to read any header value.
367376
368377
369-
### Example: Monitoring the requests and responses via the WebResourceResponseReceived event
378+
<!-- ------------------------------ -->
379+
#### Example: Monitoring the requests and responses via the WebResourceResponseReceived event
370380
371381
This example shows how to read the authorization header value by monitoring the requests and responses via the `WebResourceResponseReceived` event.
372382
@@ -376,10 +386,10 @@ The following code demonstrates how the `WebResourceResponseReceived` event can
376386
377387
<!-- -------------------------------------------------- -->
378388
379-
# [.NET](#tab/dotnet)
389+
##### [.NET](#tab/dotnet)
380390
381391
```csharp
382-
WebView.WebResourceResponseReceived += WebView_WebResourceResponseReceived;
392+
WebView.CoreWebView2.WebResourceResponseReceived += CoreWebView2_WebResourceResponseReceived;
383393
384394
// Note: modifications made to request are set but have no effect on WebView processing it.
385395
private async void WebView_WebResourceResponseReceived(object sender, CoreWebView2WebResourceResponseReceivedEventArgs e)
@@ -422,7 +432,7 @@ private async void WebView_WebResourceResponseReceived(object sender, CoreWebVie
422432

423433
<!-- -------------------------------------------------- -->
424434

425-
# [Win32](#tab/win32)
435+
##### [Win32](#tab/win32)
426436

427437
COM example, uses `ICoreWebView2WebResourceRequest`.
428438

@@ -471,7 +481,7 @@ m_webView->add_WebResourceResponseReceived(
471481
472482
<!-- -------------------------------------------------- -->
473483
474-
# [.NET](#tab/dotnet)
484+
##### [.NET](#tab/dotnet)
475485
476486
477487
**Request:**
@@ -513,7 +523,7 @@ m_webView->add_WebResourceResponseReceived(
513523
514524
<!-- -------------------------------------------------- -->
515525
516-
# [Win32](#tab/win32)
526+
##### [Win32](#tab/win32)
517527
518528
**Request:**
519529

0 commit comments

Comments
 (0)