Android Share cookie with Webview and Volley request



我已经搜索了很多答案并实现了一个帮助我在凌空请求中维护cookie的答案。

帮助我的代码正在遵循

CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));

该代码帮助我与所有凌空请求保持相同的会话。

在此代码之后,我onCreate方法中打开一个 Web 视图。Webview 具有不同的会话,并且不与凌空请求共享。

我怎样才能确保两者具有相同的会话。

以下是我的onCreate方法

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webview);
/* Dialog view */
LinearLayout linearLayoutProgressBar = findViewById(R.id.linearLayoutProgressBar);
// Creates instance of the manager.
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setSaveFormData(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
WebViewClientImpl webViewClient = new WebViewClientImpl(this, linearLayoutProgressBar);
webView.setWebViewClient(webViewClient);
webAppInterface = new WebAppInterface(this, webView);
webView.addJavascriptInterface(webAppInterface, "Android");
webView.loadUrl(AppConstants.mobileUrl);
//GetLocation function fetches the location and sends it to server.
//So first webview is opened and then data is sent to server.
getLocation();
}

谢谢

另一种解决方案是使用CookieManager.问题是,根据所使用的上下文,有许多CoockieManager。使用凌空抽射是java.net.CookieManager,而使用WebView是android.webkit.CookieManager。两者彼此不兼容。 我在这里找到的解决方案是创建您自己的 CookieManager 扩展自java.net.CookieManager.目标是同时将请求发送到android.webkit.CookieManager。像这样,两者是同步的。

Juste 按原样创建以下类:

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

最后,只调用一次,在你的代码早期和任何https请求之前:

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

在寻找很长时间的解决方案后,我在这里找到了它:HttpURLConnection(java.net.CookieManager(和WebView(android.webkit.CookieManager(之间的双向cookie同步

从凌空请求中,您必须获取饼干并存储在共享首选项中。

然后,您可以使用网络视图设置设置相同的 Cookie。

要从凌空抽射中检索 cookie 详细信息,请参阅此链接

要将 cookie 设置为网络视图 URL,例如

CookieManager cookieManager = CookieManager.getInstance();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.setAcceptThirdPartyCookies(webView, true);
} else {
cookieManager.setAcceptCookie(true);
}
cookieManager.setCookie(url, ${cookie});
webView.load(url);

最新更新