Android - 在 webview 中登录后提取 cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11100086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - extracting cookies after login in webview
提问by elgoog
I have an application that opens a url in a webview, the user must then login to a site through the webview and receives a cookie once logged in. I'm having problems getting cookies after login.
我有一个在 webview 中打开 url 的应用程序,然后用户必须通过 webview 登录到一个站点,并在登录后接收一个 cookie。登录后我在获取 cookie 时遇到问题。
The problem is, I can achieve this using android.webkit.CookieManager, and output all cookies in a single string.
问题是,我可以使用 android.webkit.CookieManager 实现这一点,并在单个字符串中输出所有 cookie。
However, I want to achieve it using the a cookie store (as in java.net.CookieStore) so I need to be using java.net.CookieManager.
但是,我想使用 cookie 存储(如在 java.net.CookieStore 中)来实现它,所以我需要使用 java.net.CookieManager。
I'm using the following code within the onPageFinished() of a WebViewClient. I know the issue is with opening a new connection, where I need to be getting the content from the current page. I'd appreciate some help, thanks
我在 WebViewClient 的 onPageFinished() 中使用以下代码。我知道问题在于打开一个新连接,我需要从当前页面获取内容。我很感激一些帮助,谢谢
@Override
public void onPageFinished(WebView view, String url){
Log.d(TAG, "Finished loading: " + url);
CookieSyncManager syncManager = CookieSyncManager.createInstance(Main.this);
syncManager.sync();
CookieManager manager = new CookieManager();
manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(manager);
try {
URL blah = new URL(url);
HttpURLConnection con = (HttpURLConnection) blah.openConnection();
readStream(con.getInputStream()); // outputting html
}
catch (Exception e) {
}
CookieStore cookieJar = manager.getCookieStore();
List<HttpCookie> cookies = cookieJar.getCookies();
for (HttpCookie cookie: cookies) {
Log.d(TAG, "cookie name : "+cookie.getName().toString());
}
}
回答by mr.boyfox
You can extract all cookies current url by this way from webview as string:
您可以通过这种方式从 webview 中提取所有 cookie 当前 url 作为字符串:
@Override
public void onPageFinished(WebView view, String url){
String cookies = CookieManager.getInstance().getCookie(url);
Log.d(TAG, "All the cookies in a string:" + cookies);
}
回答by vimal1083
It was a quite late , but it might help someone
已经很晚了,但它可能会帮助某人
you can get the cookie value using this
您可以使用此获取 cookie 值
getCookie("http://www.example.com","cookieName");
Declare the function as
将函数声明为
public String getCookie(String siteName,String cookieName){
String CookieValue = null;
CookieManager cookieManager = CookieManager.getInstance();
String cookies = cookieManager.getCookie(siteName);
String[] temp=cookies.split(";");
for (String ar1 : temp ){
if(ar1.contains(cookieName)){
String[] temp1=ar1.split("=");
CookieValue = temp1[1];
break;
}
}
return CookieValue;
}
回答by Juan Mendez
This answer is derived from @vimal1083. It returns the values in a Map
, and of course written in Kotlin
.
这个答案来自@vimal1083。它返回 a 中的值Map
,当然用Kotlin
.
fun getCookieMap(siteName: String): Map<String,String> {
val manager = CookieManager.getInstance()
val map = mutableMapOf<String,String>()
manager.getCookie(siteName)?.let {cookies ->
val typedArray = cookies.split(";".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
for (element in typedArray) {
val split = element.split("=".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
if(split.size >= 2) {
map[split[0]] = split[1]
} else if(split.size == 1) {
map[split[0]] = ""
}
}
}
return map
}
回答by Darpan
Check this link - Pass cookies from HttpURLConnection (java.net.CookieManager) to WebView (android.webkit.CookieManager)
检查此链接 - 从 HttpURLConnection (java.net.CookieManager) 到 WebView (android.webkit.CookieManager) 传递 cookie
If you want to get cookies from webview, you will have to use android.webkit.CookieManager
, from any HttpUrlConnection
, however, you can extract cookies useing java.net.CookieStore
如果您想从 webview 获取 cookie,则必须使用android.webkit.CookieManager
, from any HttpUrlConnection
,但是,您可以使用java.net.CookieStore
You will need to parse the string where you are getting all the cookies.
您将需要解析获取所有 cookie 的字符串。