【问题标题】:UWP Xamarin C# - WebView go back navigation return page expiredUWP Xamarin C# - WebView 返回导航返回页面已过期
【发布时间】:2019-03-10 19:28:31
【问题描述】:

应用显示一个带有 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 的类似配置,但没有找到任何相关信息。

有人可以帮助我吗? 提前谢谢你。

【问题讨论】:

    标签: c# xamarin webview xamarin.forms uwp


    【解决方案1】:

    我解决了添加这行代码并将 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

    【讨论】:

      猜你喜欢
      • 2021-03-25
      • 1970-01-01
      • 2017-07-08
      • 1970-01-01
      • 2015-10-28
      • 2016-03-22
      • 1970-01-01
      • 1970-01-01
      • 2014-07-03
      相关资源
      最近更新 更多