【问题标题】:Trying to load cookies with url in android webview尝试在 android webview 中加载带有 url 的 cookie
【发布时间】:2015-12-24 02:51:12
【问题描述】:

我正在尝试在 android web 视图中加载 cookie。以下是我的java代码。

final String link = "http://www.iitjeeacademy.com/mobile/question/CHE/AAH";

final String domain = "iitjeeacademy.com";

String authToken = LoginService.getCookie();

String cookie = "auth-token=" + authToken + "; csrf-token=mobile";

webView = (WebView) findViewById(R.id.webView);

Map<String, String> cookieMap = new HashMap<String, String>();

cookieMap.put("Cookie", cookie);


android.webkit.CookieSyncManager.createInstance(QuestionActivity.this);
android.webkit.CookieManager.getInstance().setAcceptCookie(true);

WebkitCookieManagerProxy coreCookieManager = new WebkitCookieManagerProxy(null, java.net.CookiePolicy.ACCEPT_ALL);

java.net.CookieHandler.setDefault(coreCookieManager);

android.webkit.CookieSyncManager.getInstance().sync();

final WebSettings settings = webView.getSettings();
settings.setLoadsImagesAutomatically(true);
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);

webView.setWebViewClient(new CustomBrowser());
webView.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);
webView.loadUrl(link, cookieMap);

private class CustomBrowser extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url){
        view.loadUrl(url);
        return true;
    }
}

WebView layout.xml

<WebView
    android:id="@+id/webView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

我尝试为自定义 cookie 存储实现 WebkitCookieManagerProxy,如 -> Pass cookies from HttpURLConnection (java.net.CookieManager) to WebView (android.webkit.CookieManager) 所示。

但它不起作用。当我运行此代码时,在 Web 视图中我被重定向到网站的索引页面...

【问题讨论】:

    标签: android android-webview session-cookies android-cookiemanager


    【解决方案1】:
     WebView webview = (WebView) this.findViewById(R.id.webView);
        final WebSettings settings = webview.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setAppCacheEnabled(true);
        settings.setBuiltInZoomControls(true);
        settings.setPluginState(WebSettings.PluginState.ON);
    
        webview.setWebChromeClient(new WebChromeClient());
    
        CookieSyncManager.createInstance(YourClass.this);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeSessionCookie();
        cookieManager.setCookie("your domain name", cookie);
        CookieSyncManager.getInstance().sync();
    
        Map<String, String> header = new HashMap<String, String>();
        header.put("Cookie", cookie);
        if(isConnected()) {
            webview.loadUrl(url, header);
        }
        else {
            webview.setVisibility(View.GONE);
            textView.setText("Your custom error message.");
            textView.setVisibility(View.VISIBLE);
        }
    

    【讨论】:

    • 为什么是textView.setText("Your custom error message.");?它是从哪里来的?
    • 实际上在我的代码中,我首先检查用户是否连接到互联网,如果没有,webview 会向用户显示 url,这在某些情况下可能会有风险,这完全取决于你想要的方式使用这段代码,主 webview cookie 代码以 loadUrl() 方法结束
    • 不。这不起作用...仍然重定向到主页:(
    • 如果在用户登录后你得到一些 cookie,你需要将它们保存在 Shared Preferences 并在 webview 中使用相同的 cookie
    • 是的,我使用的是相同的 cookie。但它仍然无法正常工作。
    【解决方案2】:

    **这可能会有所帮助**

    WebView webview = (WebView) this.findViewById(R.id.wv_file);
        final WebSettings settings = webview.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setAppCacheEnabled(true);
        settings.setBuiltInZoomControls(true);
        settings.setPluginState(WebSettings.PluginState.ON);
    
        webview.setWebChromeClient(new WebChromeClient());
    
        CookieSyncManager.createInstance(ActivityName.this);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeSessionCookie();
        String cookieString = "param=value";
        cookieManager.setCookie(domain_of_url("domain name"), cookieString);
        CookieSyncManager.getInstance().sync();
    
        Map<String, String> abc = new HashMap<String, String>();
        abc.put("Cookie", cookieString);
        webview.loadUrl("domain name",
                abc);
    

    你能得到解决办法吗

    也可以通过这个链接获得正确的想法Android WebView Cookie Problem

    或者你也可以像这些问题中那样做 Using session cookies with android

    Using cookies with Android

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-31
      • 1970-01-01
      • 1970-01-01
      • 2012-10-08
      • 1970-01-01
      • 2017-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多