我有服务器数量的 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/