IP 地址的 Android Cookie

标签 android cookies webview ip

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

但我无法解决的问题是,如果我有 IP 地址作为我的服务器主机 URL,然后尝试使用 CookieManager.getInstance().getCookie(ipAddress) 检索 cookie 返回 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'

一旦到位,这就是我们设置 cookie 以在 webview 和 OkHttp 之间共享的方式。我确信默认的 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)。

关于IP 地址的 Android Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32125287/

相关文章:

安卓 SDK Java : not found

php - 为什么未设置 magento 前端 cookie

c# - 如何在 asp.net 中的 Handler 中设置 session 值

ios - 无法使用 swift 2 在 IOS9 上的 webview 中加载 http 授权网站 url

Android:仅阻止单个 Activity 访问互联网

android - 如何使用 USB 网络摄像头修复 Android 中的拔出 USB 电缆错误?

java - 自定义ListView不显示

android - 如何将android应用程序连接到 Node 服务器?

javascript - 添加超时功能以通过 cookie 显示模态

javascript - ANDROID:网站加载不正确,某些链接在 webview 中不起作用