Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Cookies for IP address

I have HttpSessions with number of servers. The main issue was that if I logged in via WebView then my cookies is not available for the http connection. I fixed it with retrieving cookie for my URL and setting it as Cookie header for all my http connection to that URL.

But the problem I can't solve is that if I have IP address as my server host URL, then trying to retrieve cookie with CookieManager.getInstance().getCookie(ipAddress) returns null.

Should I made alias for my ip server host? Or maybe there is any solution from client side, cause in my iOS application it works nice.

One thing - I haven't tried on the IP address only yet. My IP address also contains port number, but cookies are not port-abailable, so I think they should be available with getCookie("http://MY_IP"), but they are not. But I think that it can be new surprise and port number can break the logic.

like image 254
Orest Savchak Avatar asked Oct 31 '22 21:10

Orest Savchak


1 Answers

In our app we came across the same issue. We ended up using Square's OkHttp library which is faster and easier than default android libs. You can add it by adding these lines to your build.gradle:

compile 'com.squareup.okhttp:okhttp-urlconnection:2.1.0'
compile 'com.squareup.okhttp:okhttp:2.1.0'

Once that's in place, this is how we set the cookies to be shared between the webview and OkHttp. I'm sure there's a way to do this with the default Http client as well:

public static OkHttpClient getClient(){
    OkHttpClient okHttpClient = new OkHttpClient();
    //set cookies as shared across webview and web requests
    WebkitCookieManagerProxy coreCookieManager = new WebkitCookieManagerProxy(null,
            java.net.CookiePolicy.ACCEPT_ALL);
    java.net.CookieHandler.setDefault(coreCookieManager);

    okHttpClient.setCookieHandler(coreCookieManager);
    return okHttpClient;
}

WebkitCookieManagerProxy looks like this:

//sets cookies as the same across WebView and HttpUrlConnection
public class WebkitCookieManagerProxy extends CookieManager{
private android.webkit.CookieManager webkitCookieManager;

public WebkitCookieManagerProxy()
{
    this(null, null);
}

public WebkitCookieManagerProxy(CookieStore store, CookiePolicy cookiePolicy)
{
    super(null, cookiePolicy);

    this.webkitCookieManager = android.webkit.CookieManager.getInstance();
}

@Override
public void put(URI uri, Map<String, List<String>> responseHeaders) throws IOException
{
    // make sure our args are valid
    if ((uri == null) || (responseHeaders == null)) return;

    // save our url once
    String url = uri.toString();

    // go over the headers
    for (String headerKey : responseHeaders.keySet())
    {
        // ignore headers which aren't cookie related
        if ((headerKey == null) || !(headerKey.equalsIgnoreCase("Set-Cookie2") || headerKey.equalsIgnoreCase("Set-Cookie"))) continue;

        // process each of the headers
        for (String headerValue : responseHeaders.get(headerKey))
        {
            this.webkitCookieManager.setCookie(url, headerValue);
        }
    }
}

@Override
public Map<String, List<String>> get(URI uri, Map<String, List<String>> requestHeaders) throws IOException
{
    // make sure our args are valid
    if ((uri == null) || (requestHeaders == null)) throw new IllegalArgumentException("Argument is null");

    // save our url once
    String url = uri.toString();

    // prepare our response
    Map<String, List<String>> res = new java.util.HashMap<String, List<String>>();

    // get the cookie
    String cookie = this.webkitCookieManager.getCookie(url);

    // return it
    if (cookie != null) res.put("Cookie", Arrays.asList(cookie));
    return res;
}

@Override
public CookieStore getCookieStore()
{
    // we don't want anyone to work with this cookie store directly
    throw new UnsupportedOperationException();
}
}

Now, when you log in with the webview, the cookies will be shared with the http requests (if you use OkHttpClient).

like image 150
Caleb_Allen Avatar answered Nov 11 '22 09:11

Caleb_Allen