首页 > 解决方案 > 带有代理超时的 Android Webview

问题描述

我在将 Xamarin.Android 的 webview 与代理结合使用时遇到了问题。

设想:

Xamarin.Android 应用程序仅包含尝试加载“ http://google.com ”的 WebView。

当我在没有代理的 wifi 上时,它工作正常。

当我的 wifi 使用代理访问互联网时,它超时,wifi 连接的代理设置没有得到 webview 的尊重。使用平板电脑上的 Chrome 浏览器,它可以正常工作。

我尝试了所有我能找到的东西,但似乎没有任何效果......这些方法已经过时并且返回 null 或端口 -1。

Android.Net.Proxy.GetHost(this.ApplicationContext);
Android.Net.Proxy.GetPort(this.ApplicationContext);
Java.Lang.JavaSystem.GetProperty("http.proxyPort");
Java.Lang.JavaSystem.GetProperty("http.proxyHost");

所以我的问题是:如何让 webview 与 wifi 上设置的代理一起工作?

由于一个项目的问题,我被阻止了一周,非常感谢我能得到的每一个帮助。

代码:

        // Inflate layout and find view
        SetContentView(Resource.Layout.cas_login);
        WebView wv = FindViewById<WebView>(Resource.Id.casWebView);

        // Get URLs
        string sessionId = UVM.GetJSessionId();
        string apiUri = App.GetApiURI();
        string websiteUrl = App.GetWebsiteUri();
        string websiteRedirectUrl = websiteUrl + "api/myInfo/infos";
        string requestUrl = $"{apiUri}login?requestUrl={websiteRedirectUrl}";

        // Setup webView
        wv.Settings.JavaScriptEnabled = true;
        wv.Settings.BuiltInZoomControls = true;
        wv.Settings.DisplayZoomControls = true;
        wv.Settings.DomStorageEnabled = true;
        wv.Settings.DatabaseEnabled = true;
        wv.Settings.AllowContentAccess = true;
        wv.Settings.SetAppCacheEnabled(true);
        wv.Settings.UserAgentString = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36";
        wv.Settings.MixedContentMode = MixedContentHandling.AlwaysAllow;

        wv.SetWebViewClient(new CasWebViewClient(websiteRedirectUrl, ShowMainActivity));
        //wv.SetWebChromeClient(new WebChromeClient());

        WebView.SetWebContentsDebuggingEnabled(true);
        WebView.EnablePlatformNotifications();

        CookieManager.Instance.SetCookie(websiteUrl, sessionId);

        // Load URL
        _logger.LogMessage(LogLevel.Debug, "CasLogin.cs OnCreate", "Loading page: " + requestUrl);
        wv.LoadUrl(requestUrl);

PS:如果我使用 WebChromeClient 它可以正常工作,它会打开 chrome 并且页面加载正常。

标签: androidxamarinxamarin.androidhttp-proxy

解决方案


推荐阅读