Пример #1
0
        protected override async Task HandleWebResourceRequest(CoreWebView2WebResourceRequestedEventArgs eventArgs)
        {
            // Unlike server-side code, we get told exactly why the browser is making the request,
            // so we can be smarter about fallback. We can ensure that 'fetch' requests never result
            // in fallback, for example.
            var allowFallbackOnHostPage =
                eventArgs.ResourceContext == CoreWebView2WebResourceContext.Document ||
                eventArgs.ResourceContext == CoreWebView2WebResourceContext.Other;                 // e.g., dev tools requesting page source

            // Get a deferral object so that WebView2 knows there's some async stuff going on. We call Complete() at the end of this method.
            using var deferral = eventArgs.GetDeferral();

            var requestUri = QueryStringHelper.RemovePossibleQueryString(eventArgs.Request.Uri);

            // First, call into WebViewManager to see if it has a framework file for this request. It will
            // fall back to an IFileProvider, but on WinUI it's always a NullFileProvider, so that will never
            // return a file.
            if (TryGetResponseContent(requestUri, allowFallbackOnHostPage, out var statusCode, out var statusMessage, out var content, out var headers) &&
                statusCode != 404)
            {
                // NOTE: This is stream copying is to work around a hanging bug in WinRT with managed streams.
                // See issue https://github.com/microsoft/CsWinRT/issues/670
                var memStream = new MemoryStream();
                content.CopyTo(memStream);
                var ms = new InMemoryRandomAccessStream();
                await ms.WriteAsync(memStream.GetWindowsRuntimeBuffer());

                var headerString = GetHeaderString(headers);
                eventArgs.Response = _coreWebView2Environment !.CreateWebResourceResponse(ms, statusCode, statusMessage, headerString);
            }
        private void BrowserWebResourceRequestedAsync(object sender, CoreWebView2WebResourceRequestedEventArgs e)
        {
            var coreWebView2 = (CoreWebView2)sender;

            Action <ResourceResponse, CoreWebView2Deferral> callback = (ResourceResponse resourceResponse, CoreWebView2Deferral deferral) =>
            {
                try
                {
                    var response = coreWebView2.Environment
                                   .CreateWebResourceResponse(resourceResponse.Stream,
                                                              resourceResponse.StatusCode,
                                                              resourceResponse.ReasonPhrase,
                                                              resourceResponse.GetHeaderString());
                    if (response != null)
                    {
                        e.Response = response;
                    }
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception.Message);
                    Console.WriteLine(exception.StackTrace);
                }
                finally
                {
                    deferral?.Complete();
                    deferral = null;
                }
            };


            var request = new ResourceRequest(e.Request.Uri, e.Request.Method, e.Request.Headers, e.Request.Content);

            _owinResourceHandler.ProcessRequest(request, e.GetDeferral(), callback);
        }
        private async void BrowserWebResourceRequestedAsync(object sender, CoreWebView2WebResourceRequestedEventArgs e)
        {
            var deferral = e.GetDeferral();

            var request = new ResourceRequest(e.Request.Uri, e.Request.Method, e.Request.Headers, e.Request.Content);

            var response = await RequestInterceptor.ProcessRequest(_appFunc, request);

            var coreWebView2 = (CoreWebView2)sender;

            e.Response = coreWebView2.Environment.CreateWebResourceResponse(response.Stream, response.StatusCode, response.ReasonPhrase, response.GetHeaderString());

            deferral.Complete();
        }
Пример #4
0
        private async void Webview_WebResourceRequested(object?sender, CoreWebView2WebResourceRequestedEventArgs e)
        {
            using var deferral = e.GetDeferral();

            var uri           = new Uri(e.Request.Uri);
            var contentStream = await Application.ContentProvider.GetStreamAsync(uri);

            if (contentStream == null)
            {
                e.Response = webview.CoreWebView2.Environment.CreateWebResourceResponse(null, 404, "Not Found", string.Empty);
            }
            else
            {
                e.Response = webview.CoreWebView2.Environment.CreateWebResourceResponse(contentStream, 200, "OK", string.Empty);
                e.Response.Headers.AppendHeader("Content-Type", MimeTypes.FindForUri(uri));
            }
        }
 public IDeferralWrapper GetDeferral()
 {
     return(new DeferralWrapper(_webResourceRequestedEventArgs.GetDeferral()));
 }