UWP Xamarin C#-WebView返回导航返回页面已过期

时间:2018-10-05 10:02:01

标签: c# xamarin webview xamarin.forms uwp

该应用程序显示带有WebView的网站。 该站点有一个“返回”按钮来导航,但是当我点击它时会显示此错误:

  

网页已过期。主要原因:电话上的网页副本未更新,网站请求更新以查看   信息

我的WebView请求代码:

var request = new Windows.Web.Http.HttpRequestMessage(Windows.Web.Http.HttpMethod.Post, new Uri("http://example.com/index.php"));

IHttpContent content = new HttpFormUrlEncodedContent(new [] { (new KeyValuePair<string, string>("param", myParam)) });
request.Content = content;
webView.NavigateWithHttpRequestMessage(request);

我在Xamarin.Android应用上遇到了类似的问题,并且解决了将此代码添加到WebView:

webView.Settings.SetAppCacheEnabled(true);
webView.Settings.SetAppCacheMaxSize(8 * 1024 * 1024);
webView.Settings.CacheMode = CacheModes.CacheElseNetwork;

我试图为UWP看到类似的配置,但没有找到任何关于它的信息。

有人可以帮助我吗? 预先谢谢你。

1 个答案:

答案 0 :(得分:0)

我解决了添加以下代码行并将webView.NavigateWithHttpRequestMessage(request)更改为webView.Navigate(uri):

                var myFilter = new HttpBaseProtocolFilter();
                myFilter.AllowAutoRedirect = true;
                myFilter.CacheControl.ReadBehavior = HttpCacheReadBehavior.Default;
                myFilter.CacheControl.WriteBehavior = HttpCacheWriteBehavior.Default;
                var cookieManager = myFilter.CookieManager;

                var request = new Windows.Web.Http.HttpRequestMessage(Windows.Web.Http.HttpMethod.Post, new Uri("http://example.com/index.php"));
                IHttpContent content = new HttpFormUrlEncodedContent(new [] { (new KeyValuePair<string, string>("param", myParam)) });
                request.Content = content;

                using (var client = new Windows.Web.Http.HttpClient())
                {
                    var result = await client.SendRequestAsync(request);
                    result.EnsureSuccessStatusCode();
                    var resultContent = await result.Content.ReadAsStringAsync();
                }

                webView.Settings.IsJavaScriptEnabled = true;
                webView.Settings.IsIndexedDBEnabled = true;

                webView.Navigate(new Uri("http://example.com/index.php"));

链接到-> https://dzone.com/articles/sharing-sessions-between-httpclient-and-webviews-o