Android Cookie的IP地址 [英] Android Cookies for IP address

查看:296
本文介绍了Android Cookie的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

但我无法解决的问题是,如果我有IP地址作为我的服务器主机URL,然后尝试检索Cookie与CookieManager.getInstance()。getCookie ipAddress)返回null。

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.

我应该为我的ip服务器主机做别名吗?或者也许有从客户端的任何解决方案,因为在我的iOS应用程序它工作不错。

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.

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

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.

推荐答案

在我们的应用程序,问题。我们最终使用 Square的OkHttp库,它比默认的Android库更快更容易。你可以通过添加这些行到你的build.gradle添加它:

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'

一旦到位,这是我们如何设置cookie在Webview和OkHttp之间共享。我确定有一个方法来做到这一点与默认Http客户端:

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外观如下:

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

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

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

这篇关于Android Cookie的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆