【问题标题】:Share created session between HTTPURLCONNECTION and WebView在 HTTPURLCONNECTION 和 WebView 之间共享创建的会话
【发布时间】:2017-05-06 03:15:09
【问题描述】:

我很难找到解决问题的方法。我使用 HttpsUrlConnection 开始从 android 到 php 文件的网络服务器会话。这是我在登录 Web 服务器时用来设置 cookie 以使会话正常工作的代码:

CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);

这似乎使我的会话正常工作,这正是我想要的。但是,我有一个问题。在我的应用程序的一部分中,我正在使用一个 web 视图来访问 过去 在线登录页面的页面。因此,为了让它工作,我需要以某种方式使用 HttpsUrlConnection 发送与我的 JSONObject 的会话,以便我可以绕过身份验证。

这是我用于 WebView 的代码:

webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);      
webView.loadUrl(URL);

它可以很好地加载页面,但它不使用 HttpsUrlConnection 使用的会话。我可以一遍又一遍地使用 HttpsUrlConnection,它会绕过身份验证,因为它使用 cookie 存储并且 php 文件会记住会话。我无法弄清楚如何在 Web 视图中以相同的方式传递这些 cookie 或会话,以便我可以正确使用该页面。

任何帮助将不胜感激。

编辑:我通过创建一个自定义 cookie 管理器类来解决这个问题,该类共享 cookiemanager 和 webview cookie 管理器的 cookie

【问题讨论】:

    标签: php android session cookies webview


    【解决方案1】:

    这是因为 WebView 有自己的 CookieStore,称为 android.net.CookieManager。你创建了一个 java.net.Cookiemanager 的实例。 他们彼此不认识。您必须在两者之间交换 cookie。

    1.) 创建您自己的 java.net.CookieManager 实例,获取 android.net.CookieManager 实例:

    public class MyCookieManager extends CookieManager  {
    
    private android.webkit.CookieManager webkitCookieManager = null;
    
    /**
     * Constructor
     * @param cookieManager android.webkit.CookieManager
     * @param cookiePolicy CookiePolicy
     */
    public  MyCookieManager (android.webkit.CookieManager cookieManager, CookiePolicy cookiePolicy) {
    
        super(null, cookiePolicy);
        this.webkitCookieManager = cookieManager;
        //Cookies are allowed
        this.webkitCookieManager.setAcceptCookie(true);
    }
    
    /**
      * @param uri URI
     * @param responseHeaders Map<String, List<String>>
     */
    @Override
    public void put(URI uri, Map<String, List<String>> responseHeaders) {
    
        if (responseHeaders == null) {
    
            return;
        }
    
        for (String headerKey : responseHeaders.keySet()) {
    
            if ( headerKey == null || "Set-Cookie".equalsIgnoreCase(headerKey) == false) {
    
                continue;
            }
    
    
            for (String headerValue : responseHeaders.get(headerKey)) {
    
    
                webkitCookieManager.setCookie(uri.toString(), headerValue);
            }
        }
    
    }
    
    /**
     * @param uri URI
     * @param requestHeaders Map<String, List<String>>
     * @return Map<String, List<String>>
     */
    @Override
    public Map<String, List<String>> get(URI uri, Map<String, List<String>> requestHeaders) {   
    
        if (requestHeaders == null) {
    
            return null;
        }
    
        Map<String, List<String>> res = new java.util.HashMap<String, List<String>>();
    
        // get cookies from Webview CookieManager
        String cookie = webkitCookieManager.getCookie(uri.toString());
    
        if (cookie == null)  {
    
            cookie = "";
        }
        res.put("Cookie", Arrays.asList(cookie));
    
        return res;
    }
    }
    

    2.) 在您的应用程序类中是这样的:

    public class ApplicationWrapper extends Application {
    
        private MyCookieManager manager = null;
    
        @Override
        public void onCreate() {
    
            //some work...
            this.initCookieManager();
            //some more work...
        }
    
        private void initCookieManager () {
    
        this.manager = new MyCookieManager(
                CookieManager.getInstance(),
                java.net.CookiePolicy.ACCEPT_ALL);
        CookieHandler.setDefault(this.manager);
    }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-28
      • 2014-08-10
      • 2022-01-24
      • 2014-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-20
      相关资源
      最近更新 更多