gpt4 book ai didi

android - 将 Cookie 从 HttpURLConnection (java.net.CookieManager) 传递到 WebView (android.webkit.CookieManager)

转载 作者:IT老高 更新时间:2023-10-28 21:50:24 34 4
gpt4 key购买 nike

I've seen answers about how this should work with the old DefaultHttpClient but there's not a good example for HttpURLConnection

我正在使用 HttpURLConnection 向 Web 应用程序发出请求。在我的 Android 应用程序开始时,我使用 CookieHandler.setDefault(new CookieManager()) 来自动处理 session cookie,这工作正常。

在登录后的某个时间点,我想使用 WebView 向用户显示来自 Web 应用程序的实时页面,而不是使用 HttpURLConnection 在后台下载数据。但是,我想使用我之前建立的同一个 session 来防止用户再次登录。

如何将 HttpURLConnection 使用的 java.net.CookieManager 中的 cookie 复制到 使用的 android.webkit.CookieManager WebView 以便我可以共享 session ?

最佳答案

我想建议一种完全不同的方法来解决您的问题。与其将 cookie 从一个地方复制到另一个地方(手动同步),不如让 HttpURLConnection 和 WebViews 使用 same cookie 存储。

这完全消除了同步的需要。在其中任何一个中更新的任何 cookie 都会立即自动反射(reflect)在另一个中。

为此,创建您自己的 java.net.CookieManager 实现,它将所有请求转发到 WebViews 的 webkit android.webkit.CookieManager。

实现:

import java.io.IOException;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.CookieStore;
import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class WebkitCookieManagerProxy extends CookieManager
{
private android.webkit.CookieManager webkitCookieManager;

public WebkitCookieManagerProxy()
{
this(null, null);
}

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

最后在你的应用程序初始化中使用它:

android.webkit.CookieSyncManager.createInstance(appContext);
// unrelated, just make sure cookies are generally allowed
android.webkit.CookieManager.getInstance().setAcceptCookie(true);

// magic starts here
WebkitCookieManagerProxy coreCookieManager = new WebkitCookieManagerProxy(null, java.net.CookiePolicy.ACCEPT_ALL);
java.net.CookieHandler.setDefault(coreCookieManager);

关于android - 将 Cookie 从 HttpURLConnection (java.net.CookieManager) 传递到 WebView (android.webkit.CookieManager),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12731211/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com