Exemple #1
0
        internal async void WebResourceRequested(WebView sender, WebViewWebResourceRequestedEventArgs args)
        {
            var    request   = args.Request;
            string localPath = request.RequestUri.LocalPath;

            if (localPath.StartsWith("/kcs2/index.php"))
            {
                referer = request.RequestUri;
            }
            else if (localPath.StartsWith("/kcsapi/"))
            {
                var deferral = args.GetDeferral();
                try
                {
                    string reqStr = null;
                    if (request.Content != null)
                    {
                        using var stream = new InMemoryRandomAccessStream();
                        await request.Content.WriteToStreamAsync(stream);

                        stream.Seek(0);
                        reqStr          = new StreamReader(stream.AsStream()).ReadToEnd();
                        request.Content = new HttpStringContent(reqStr, UnicodeEncoding.Utf8, "application/x-www-form-urlencoded");
                    }
                    request.Headers.Referer = referer;
                    using (request.Content)
                        using (var response = await client.SendRequestAsync(request))
                        {
                            args.Response = response;
                            deferral.Complete();

                            Received?.Invoke(response.Headers.Date ?? DateTimeOffset.Now, new HttpMessage
                                             (
                                                 localPath.Substring(8),
                                                 reqStr.AsMemory(),
                                                 (await response.Content.ReadAsStringAsync()).AsMemory(7)
                                             ));
                        }
                }
                catch
                {
                    args.Response = new HttpResponseMessage(HttpStatusCode.BadGateway);
                    deferral.Complete();
                }
            }
        }
Exemple #2
0
        private void WebView_WebResourceRequested(object sender, WebViewWebResourceRequestedEventArgs e)
        {
            var reqesutUri = e.Request.RequestUri;

            using (var defferral = e.GetDeferral())
            {
                try
                {
                    var stream = PageViewModel.ResolveWebResourceRequest(reqesutUri);
                    if (stream != null)
                    {
                        e.Response         = new Windows.Web.Http.HttpResponseMessage(statusCode: Windows.Web.Http.HttpStatusCode.Ok);
                        e.Response.Content = new HttpStreamContent(stream.AsInputStream());
                    }
                }
                finally
                {
                    defferral.Complete();
                }
            }
        }
 Deferral IWebViewWebResourceRequestedEventArgsResolver.GetDeferral(WebViewWebResourceRequestedEventArgs e) => e.GetDeferral();