用于 IP 地址的 Android Cookie

Ore*_*hak 5 cookies ip android webview

我有带有服务器数量的 HttpSessions。主要问题是,如果我通过 WebView 登录,则我的 cookie 不可用于 http 连接。我通过为我的 URL 检索 cookie 并将其设置为我所有与该 URL 的 http 连接的 Cookie 标头来修复它。

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

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

一件事 - 我还没有尝试过 IP 地址。我的 IP 地址也包含端口号,但是 cookie 不是端口可用的,所以我认为它们应该可以通过 getCookie(" http://MY_IP ") 获得,但它们不是。但我认为它可以是新的惊喜,端口号可以打破逻辑。

Cal*_*len 2

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

compile 'com.squareup.okhttp:okhttp-urlconnection:2.1.0'
compile 'com.squareup.okhttp:okhttp:2.1.0'
Run Code Online (Sandbox Code Playgroud)

一旦到位,这就是我们设置在 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;
}
Run Code Online (Sandbox Code Playgroud)

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();
}
}
Run Code Online (Sandbox Code Playgroud)

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