OxenFreeHorchata
OxenFreeHorchata

Reputation: 9

Share created session between HTTPURLCONNECTION and WebView

I'm having much trouble figuring out the solution to my issue. I begin a webserver session from android to a php file using HttpsUrlConnection. Here is the code I use to set the cookies while logging in to the web server to make the session work correctly:

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

This seems to make my session work correctly, which is exactly what I wanted. I have one issue, however. In one part of my app, I am using a webview that accesses a page that is past the login page online. So, for it to work, I need to somehow send my session with my JSONObject using HttpsUrlConnection so I can bypass the authentication.

Here is the code I am using for the WebView:

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

It loads the page fine, but it does not use the session that HttpsUrlConnection uses. I can use HttpsUrlConnection over and over and it will bypass authentication as it uses the cookie storage and the php file remembers the session. I cannot figure out how to pass these cookies or session in the same way in a web view so I can use the page properly.

Any help would be greatly appreciated.

EDIT: I solved this by creating a custom cookie manager class that shares cookies for the cookiemanager and the webview cookie manager

Upvotes: 0

Views: 683

Answers (1)

A.D.
A.D.

Reputation: 1462

That's because the WebView has its own CookieStore called android.net.CookieManager. And you created an instance of java.net.Cookiemanager. They do not know each other. You have to swap the cookies between those both.

1.) Create your own instance of java.net.CookieManager which gets a instance of 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.) And in your Application-Class something like this:

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);
}

}

Upvotes: 2

Related Questions