使用webview客户端通过代理在android webview中加载URL

时间:2016-08-25 18:43:44

标签: android webview proxy webviewclient

我在WebView中加载一个URL。 URL通过代理,所以它要求登录然后让我通过页面。下次当我尝试相同的网址时,它会提供 ERR_CONNECTION_TIMED_OUT ,因为我已经登录该网址。

即使使用 CookieManger.getInstance()。acceptCookie()也无济于事。 我在做什么:

WebViewClient wvc = new WebViewClient() {

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        //progressBar.setVisibility(View.INVISIBLE);
        super.onPageStarted(view, url, favicon);
    }

    public void onPageFinished(WebView view, String url) {
        isLoaded = true;
        progressBar.setVisibility(View.INVISIBLE);


    }

    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        progressBar.setVisibility(View.INVISIBLE);
        super.onReceivedError(view, request, error);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView wView, String url) {
        progressBar.setVisibility(View.INVISIBLE);
        Log.d(getClass().getSimpleName(), "loading url: " + url);
        if(!isHomeUrlLoaded) {
           // webView.loadUrl(url);
            return false;

        } else {
            openCustomTab(url);
        }
        return true;
    }
};

private void openCustomTab(String url) {
    PackageManager manager = getActivity().getPackageManager();
    try {
        PackageInfo packageInfo = manager.getPackageInfo("com.android.chrome", PackageManager.GET_ACTIVITIES);
        String packageVersion = packageInfo.versionName;
        //packageVersion = "52.0.2623.105";
        Log.d(sTag, "Chrome version code :" + packageVersion);
        if(packageVersion.compareTo("44") >= 1) {
            CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
            CustomTabsIntent customTabsIntent = builder.build();
            customTabsIntent.intent.setPackage("com.android.chrome");
            customTabsIntent.launchUrl(getActivity(), Uri.parse(url));
        } else {
            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            i.setPackage("com.android.chrome");
            startActivity(i);
        };
    } catch (PackageManager.NameNotFoundException e) {
        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(i);
    } catch (Exception e) {

    }
}

感谢任何帮助。

0 个答案:

没有答案