|
| 1 | +# Background |
| 2 | +In the `NavigationCompleted` event, the `WebErrorStatus` property provides information about why a |
| 3 | +navigation may have failed. However, this is more geared toward network failures, and doesn't |
| 4 | +provide a comprehensive mapping of HTTP status codes. And even if it did provide an enum value |
| 5 | +for every common HTTP status code, it is still possible for a server to respond with a custom |
| 6 | +status code, which an application might want to recognize and handle. |
| 7 | + |
| 8 | +The `WebResourceResponseReceived` event does provide information about the response, including |
| 9 | +its HTTP status, but it is difficult to correlate a `WebResourceResponse` with a given navigation. |
| 10 | + |
| 11 | +There are various ways the API could be improved to provide this information, but for now we are |
| 12 | +going with the simplest approach, and adding a new property that provides an HTTP status code in |
| 13 | +the NavigationCompleted event. |
| 14 | + |
| 15 | +# Description |
| 16 | +The `NavigationCompletedEventArgs` interface will be given a new property, `HttpStatusCode`, which |
| 17 | +has the HTTP status code of the navigation if it involved an HTTP request. For instance, this will |
| 18 | +usually be 200 if the request was successful, 404 if a page was not found, etc. See |
| 19 | +https://developer.mozilla.org/en-US/docs/Web/HTTP/Status for a list of common status codes. |
| 20 | + |
| 21 | +The `HttpStatusCode` property will be 0 in the following cases: |
| 22 | +* The navigation did not involve an HTTP request. For instance, if it was a navigation to a |
| 23 | + file:// URL, or if it was a same-document navigation. |
| 24 | +* The navigation failed before a response was received. For instance, if the hostname was not |
| 25 | + found, or if there was a network error. |
| 26 | + |
| 27 | +In those cases, you can get more information from the `IsSuccess` and `WebErrorStatus` properties. |
| 28 | + |
| 29 | +If the navigation receives a successful HTTP response, but the navigated page calls |
| 30 | +`window.stop()` before it finishes loading, then `HttpStatusCode` may contain a success code like |
| 31 | +200, but `IsSuccess` will be false and `WebErrorStatus` will be `ConnectionAborted`. |
| 32 | + |
| 33 | +Since WebView2 handles HTTP continuations and redirects automatically, it is unlikely for |
| 34 | +`HttpStatusCode` to ever be in the 1xx or 3xx ranges. |
| 35 | + |
| 36 | +# Examples |
| 37 | +## Win32 C++ |
| 38 | +```c++ |
| 39 | +void NavigationCompletedSample() |
| 40 | +{ |
| 41 | + m_webview->add_NavigationCompleted( |
| 42 | + Callback<ICoreWebView2NavigationCompletedEventHandler>( |
| 43 | + [this]( |
| 44 | + ICoreWebView2* sender, |
| 45 | + ICoreWebView2NavigationCompletedEventArgs* args) |
| 46 | + { |
| 47 | + wil::com_ptr<ICoreWebView2NavigationCompletedEventArgs2> args2; |
| 48 | + args->QueryInterface(IID_PPV_ARGS(&args2)); |
| 49 | + if (args2) |
| 50 | + { |
| 51 | + int status_code; |
| 52 | + args2->get_HttpStatusCode(&status_code); |
| 53 | + if (status_code == 403) |
| 54 | + { |
| 55 | + ReportForbidden(); |
| 56 | + } |
| 57 | + else if (status_code == 404) |
| 58 | + { |
| 59 | + ReportNotFound(); |
| 60 | + } |
| 61 | + else if (status_code >= 500 && status_code <= 599) |
| 62 | + { |
| 63 | + ReportServerError(status_code); |
| 64 | + } |
| 65 | + } |
| 66 | + return S_OK; |
| 67 | + }).Get(), nullptr); |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +## .NET C# |
| 72 | +```c# |
| 73 | +void NavigationCompletedSample() |
| 74 | +{ |
| 75 | + _webview.NavigationCompleted += (object sender, CoreWebView2NavigationCompletedEventArgs args) => |
| 76 | + { |
| 77 | + int statusCode = args.HttpStatusCode; |
| 78 | + if (status_code == 403) |
| 79 | + { |
| 80 | + ReportForbidden(); |
| 81 | + } |
| 82 | + else if (status_code == 404) |
| 83 | + { |
| 84 | + ReportNotFound(); |
| 85 | + } |
| 86 | + else if (status_code >= 500 && status_code <= 599) |
| 87 | + { |
| 88 | + ReportServerError(status_code); |
| 89 | + } |
| 90 | + }; |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +# API Details |
| 95 | +## MIDL |
| 96 | +``` |
| 97 | +[uuid(6ECF0A0D-D45D-4279-B17E-6E5DC496BA38), object, pointer_default(unique)] |
| 98 | +interface ICoreWebViewNavigationCompletedEventArgs2 : ICoreWebView2NavigationCompletedEventArgs { |
| 99 | + /// The HTTP status code of the navigation if it involved an HTTP request. |
| 100 | + /// For instance, this will usually be 200 if the request was successful, 404 |
| 101 | + /// if a page was not found, etc. See |
| 102 | + /// https://developer.mozilla.org/en-US/docs/Web/HTTP/Status for a list of |
| 103 | + /// common status codes. |
| 104 | + /// |
| 105 | + /// The `HttpStatusCode` property will be 0 in the following cases: |
| 106 | + /// * The navigation did not involve an HTTP request. For instance, if it was |
| 107 | + /// a navigation to a file:// URL, or if it was a same-document navigation. |
| 108 | + /// * The navigation failed before a response was received. For instance, if |
| 109 | + /// the hostname was not found, or if there was a network error. |
| 110 | + /// |
| 111 | + /// In those cases, you can get more information from the `IsSuccess` and |
| 112 | + /// `WebErrorStatus` properties. |
| 113 | + /// |
| 114 | + /// If the navigation receives a successful HTTP response, but the navigated |
| 115 | + /// page calls `window.stop()` before it finishes loading, then `HttpStatusCode` |
| 116 | + /// may contain a success code like 200, but `IsSuccess` will be FALSE and |
| 117 | + /// `WebErrorStatus` will be `COREWEBVIEW2_WEB_ERROR_STATUS_CONNECTION_ABORTED`. |
| 118 | + /// |
| 119 | + /// Since WebView2 handles HTTP continuations and redirects automatically, it |
| 120 | + /// is unlikely for `HttpStatusCode` to ever be in the 1xx or 3xx ranges. |
| 121 | +
|
| 122 | + [propget] HRESULT HttpStatusCode([out, retval] INT* http_status_code); |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +## MIDL3 |
| 127 | +```c# (but really MIDL3) |
| 128 | +namespace Microsoft.Web.WebView2.Core |
| 129 | +{ |
| 130 | + runtimeclass CoreWebView2NavigationCompletedEventArgs |
| 131 | + { |
| 132 | + // ... |
| 133 | +
|
| 134 | + [interface_name("Microsoft.Web.WebView2.Core.ICoreWebView2NavigationCompletedEventArgs2")] |
| 135 | + { |
| 136 | + int HttpStatusCode { get; }; |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | +``` |
0 commit comments