我在标头中设置cookie并使用此标头调用WebView.loadUrl()但它(标头中的Cookie)将无法在除4.4之外的任何Android设备上运行。我在Android版本4.2,4.3,4.4,5.0和5.1上进行了测试。
webView = (WebView) findViewById(R.id.web_view);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
HashMap <String, String> extraHeaders = new HashMap<String, String>();
extraHeaders.put("Cookie", "{cookie value}");
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url, extraHeaders);
return false;
}
});
webView.loadUrl(url, extraHeaders);
答案 0 :(得分:6)
你试过这个吗?
CookieManager.getInstance().setAcceptCookie(true);
答案 1 :(得分:6)
如果您使用的是Android Lollipop,那么
CookieManager.getInstance().setAcceptCookie(true);
没有工作。你需要使用
CookieManager.getInstance().setAcceptThirdPartyCookies(true);
答案 2 :(得分:6)
设置cookie使用以下方法
CookieManager.getInstance().setCookie(BuildConfig.BASE_SERVER_ENDPOINT,COOKIE_VAL);
确保基本端点与webview中打开的链接的基本URL匹配。
删除Cookie
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
CookieManager.getInstance().removeAllCookies(null);
} else {
CookieManager.getInstance().removeAllCookie();
}
答案 3 :(得分:5)
由于Cookie政策的原因,要解决此问题,您应该添加以下内容:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.init();
// Allow third party cookies for Android Lollipop
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
WebView webView = (WebView)super.appView;
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptThirdPartyCookies(webView,true);
}
super.loadUrl(Config.getStartUrl());
}
答案 4 :(得分:5)
这只是关于向网络视图添加Cookie的快速帖子。如果你曾经尝试过按照大多数人说应该这样做的方式做到这一点,那你就失败了,发现了这篇文章。 :)
它的工作方式是你在CookieManager上设置cookie然后告诉CookieSyncManager同步。
CookieManager.getInstance().setCookie(domain, value);
CookieSyncManager.getInstance().sync();
我从来没有按照描述工作。有或没有异步任务等待线程赶上。
相反,我只是在所有loadUrl调用的标题中添加cookie。
Map<String, String> headers = new HashMap<String, String>();
headers.put("Cookie", "cookieName=cookieValue;domain=domain.com;path=/;Expires=Thu, 2 Aug 2021 20:47:11 UTC;");
webView.loadUrl("myurl.com", headers );
警告:我只需要为请求加载适当的cookie,如果要覆盖浏览器内部的嵌套调用,则需要覆盖shouldOverrideUrlLoading。
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url, headers);
return false;
}
});
如果您需要为所有请求(包括图片,js等)注入cookie,您将需要覆盖shouldInterceptRequest
,
答案 5 :(得分:2)
我通过创建自定义cookie管理器获得了该问题的解决方案:
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);
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();
}
}
在Application类或应用程序启动时初始化自定义cookie管理器:
android.webkit.CookieSyncManager.createInstance(this);
android.webkit.CookieManager.getInstance().setAcceptCookie(true);
WebkitCookieManagerProxy coreCookieManager = new WebkitCookieManagerProxy(null, java.net.CookiePolicy.ACCEPT_ALL);
java.net.CookieHandler.setDefault(coreCookieManager);
答案 6 :(得分:0)
如果您使用的是Android Lollipop,即SDK 21,那么:
CookieManager.getInstance().setAcceptThirdPartyCookies(true);
不起作用。你需要使用:
namespace :send_trending_deals do
desc "TODO"
task email_trending_deals: :environment do
@app_users = AppUser.joins(:notification).where("recieve_trending_deals = ?", true).to_a
@app_users.each do |app_user|
DealNotifier.send_trending_deal(app_user).deliver_now
puts "#{app_user.id} Email sent successfully"
end
end
end
我遇到了同样的问题,上面的一行充当了魅力。