用于IP地址的Android Cookies

我有许多服务器的HttpSessions.主要问题是如果我通过WebView登录,那么我的cookie不能用于http连接.我通过检索我的URL的cookie并将其设置为所有我与该URL的http连接的Cookie标头来修复它.

但我无法解决的问题是,如果我有IP地址作为我的服务器主机URL,那么尝试使用CookieManager.getInstance()检索cookie.getCookie(ipAddress)返回null.

我应该为我的ip服务器主机制作别名吗?或者也许客户端有任何解决方案,因为在我的iOS应用程序中它运行良好.

有一件事 – 我还没有尝试过IP地址.我的IP地址也包含端口号,但是cookie不是端口可用的,所以我认为它们应该可以与getCookie(“http://MY_IP”)一起使用,但它们不是.但我认为它可能是新的惊喜,端口号可以打破逻辑.

最佳答案 在我们的应用程序中,我们遇到了同样的问题我们最终使用
Square’s OkHttp library比默认的android库更快更容易.您可以通过将这些行添加到build.gradle来添加它:

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

一旦到位,这就是我们如何设置在webview和OkHttp之间共享的cookie.我确信有一种方法可以使用默认的Http客户端:

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看起来像这样:

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

现在,当您使用webview登录时,cookie将与http请求共享(如果您使用OkHttpClient).

点赞