[英]Android Cookies for IP address
我有許多服務器的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的OkHttp庫 ,它比默認的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)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.