Skip to content

Commit ae494c1

Browse files
committed
WebResourceResponseReceived API v2 doc Rev2
1 parent 2cf4646 commit ae494c1

1 file changed

Lines changed: 35 additions & 33 deletions

File tree

specs/WebResourceResponseReceived.md

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ event handler running.
2828

2929
When the event is raised, the WebView will pass a
3030
`WebResourceResponseReceivedEventArgs`, which lets the app view the request and
31-
response. To get the response content, call `TryGetContent` on the
32-
`CoreWebView2WebResourceResponseView` object from the event args.
31+
response. To get the response content, call `GetContent`/`GetContentAsync` on
32+
the `CoreWebView2WebResourceResponseView` object from the event args.
3333

3434
# Examples
3535
The following code snippets demonstrate how the `WebResourceResponseReceived`
@@ -50,14 +50,18 @@ m_webview->add_WebResourceResponseReceived(
5050
wil::com_ptr<ICoreWebView2WebResourceResponseView> webResourceResponse;
5151
CHECK_FAILURE(args->get_Response(&webResourceResponse));
5252
53-
// Get body content for the response. Redirect responses will
54-
// return an error HRESULT as their body (if any) is ignored.
55-
HRESULT getContentCallResult = webResourceResponse->TryGetContent(
53+
// Get body content for the response
54+
webResourceResponse->GetContent(
5655
Callback<
57-
ICoreWebView2WebResourceResponseViewTryGetContentCompletedHandler>(
56+
ICoreWebView2WebResourceResponseViewGetContentCompletedHandler>(
5857
[this, webResourceRequest, webResourceResponse](HRESULT result, IStream* content) {
59-
// The response might not have content, or it might have failed loading.
60-
bool gotContent = SUCCEEDED(result);
58+
// The response content might have failed to load.
59+
bool getContentSucceeded = SUCCEEDED(result);
60+
61+
// The stream will be null if no content was found for the response.
62+
if (content) {
63+
DoSomethingWithContent(content);
64+
}
6165
6266
std::wstring message =
6367
L"{ \"kind\": \"event\", \"name\": "
@@ -111,28 +115,27 @@ private async void WebView_WebResourceResponseReceived(CoreWebView2 sender, Core
111115
// Get response body
112116
try
113117
{
114-
System.IO.Stream content = await e.Response.TryGetContentAsync();
115-
DoSomethingWithResponseContent(content);
118+
System.IO.Stream content = await e.Response.GetContentAsync();
119+
// Null will be returned if no content was found for the response.
120+
if (content)
121+
{
122+
DoSomethingWithResponseContent(content);
123+
}
116124
}
117125
catch (COMException ex)
118126
{
119-
// A COMException will be thrown if no content was found for the
120-
// response, or if it failed to load.
127+
// A COMException will be thrown if the content failed to load.
121128
}
122129
}
123130
}
124131
```
125132

126133

127134
# Remarks
128-
Calling `ICoreWebView2WebResourceResponseView::TryGetContent` will return an
129-
error HRESULT for redirect responses, as their content (if any) is ignored.
130-
`ICoreWebView2WebResourceResponseViewTryGetContentCompletedHandler` will be
131-
invoked with a failure errorCode if no content was found for the response, or if
132-
the content failed to load.
133-
Calling `CoreWebView2WebResourceResponseView.TryGetContentAsync` will throw a
134-
`COMException` if the response has no body. As noted, the body for redirect
135-
responses is ignored.
135+
`ICoreWebView2WebResourceResponseViewGetContentCompletedHandler` will be
136+
invoked with a failure errorCode if the content failed to load.
137+
Calling `CoreWebView2WebResourceResponseView.GetContentAsync` will throw a
138+
`COMException` if the content failed to load.
136139

137140

138141
# API Notes
@@ -209,25 +212,24 @@ interface ICoreWebView2WebResourceResponseView : IUnknown
209212
[propget] HRESULT ReasonPhrase([out, retval] LPWSTR* reasonPhrase);
210213

211214
/// Get the response content asynchronously. The handler will receive the
212-
/// the response content stream. Calling this method will return an error
213-
/// HRESULT for redirect responses, as their content (if any) is ignored.
215+
/// response content stream.
214216
/// If this method is being called again before a first call has completed,
215217
/// the handler will be invoked at the same time the handlers from prior calls
216218
/// are invoked.
217219
/// If this method is being called after a first call has completed, the
218220
/// handler will be invoked immediately.
219-
HRESULT TryGetContent(
220-
[in] ICoreWebView2WebResourceResponseViewTryGetContentCompletedHandler* handler);
221+
HRESULT GetContent(
222+
[in] ICoreWebView2WebResourceResponseViewGetContentCompletedHandler* handler);
221223
}
222224

223225
/// The caller implements this interface to receive the result of the
224-
/// ICoreWebView2WebResourceResponseView::TryGetContent method.
225-
interface ICoreWebView2WebResourceResponseViewTryGetContentCompletedHandler : IUnknown
226+
/// ICoreWebView2WebResourceResponseView::GetContent method.
227+
interface ICoreWebView2WebResourceResponseViewGetContentCompletedHandler : IUnknown
226228
{
227229
/// Called to provide the implementer with the completion status and result of
228-
/// the corresponding asynchronous method call. A failure errorCode indicates
229-
/// no content was found for the response, or the content failed to load. Null
230-
/// means empty content data.
230+
/// the corresponding asynchronous method call. A failure errorCode will be
231+
/// passed if the content failed to load. Null means no content was found.
232+
/// Note content (if any) for redirect responses is ignored.
231233
HRESULT Invoke([in] HRESULT errorCode, [in] IStream* content);
232234
}
233235

@@ -281,14 +283,14 @@ namespace Microsoft.Web.WebView2.Core
281283
/// The HTTP response reason phrase.
282284
String ReasonPhrase { get; };
283285
/// Get the response content stream asynchronously.
284-
/// This method will throw a COM exception if no content was found for the
285-
/// response, or the content failed to load. Note the content (if any) for
286-
/// redirect responses is ignored. A null stream means empty data content.
286+
/// This method will throw a COM exception if the content failed to load.
287+
/// A null stream means no content was found. Note content (if any) for
288+
/// redirect responses is ignored.
287289
/// If this method is being called again before a first call has completed,
288290
/// it will complete at the same time all prior calls do.
289291
/// If this method is being called after a first call has completed, it will
290292
/// return immediately (asynchronously).
291-
Windows.Foundation.IAsyncOperation<Windows.Storage.Streams.IRandomAccessStream> TryGetContentAsync();
293+
Windows.Foundation.IAsyncOperation<Windows.Storage.Streams.IRandomAccessStream> GetContentAsync();
292294
}
293295
}
294296
```

0 commit comments

Comments
 (0)