安卓 - 在登录后的WebView提取饼干 [英] Android - extracting cookies after login in webview
问题描述
我有一个web视图中打开一个网址的应用程序,用户必须再通过web视图登录到网站,并获得一次登录的Cookie。我在登陆后获取的Cookie的问题。
现在的问题是,我可以用android.webkit.CookieManager做到这一点,并输出所有cookie在一个单一的字符串。
不过,我想用一个cookie存储(如java.net.CookieStore)来实现它,所以我需要使用java.net.CookieManager。
我使用的是WebViewClient的onPageFinished()中的以下code。我知道这个问题是打开一个新的连接,在这里我需要得到从当前页面的内容。我倒是AP preciate一些帮助,谢谢
@覆盖
公共无效onPageFinished(web视图查看,字符串URL){
Log.d(TAG,装货完毕:+网址);
CookieSyncManager syncManager = CookieSyncManager.createInstance(Main.this);
syncManager.sync();
CookieManager经理=新CookieManager();
manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(经理);
尝试 {
URL等等=新的网址(URL);
HttpURLConnection的CON =(HttpURLConnection类)blah.openConnection();
readStream(con.getInputStream()); //输出HTML
}
赶上(例外五){
}
CookieStore的cookieJar = manager.getCookieStore();
名单<的HttpCookie>饼干= cookieJar.getCookies();
对于(的HttpCookie饼干:饼干){
Log.d(TAG,cookie的名称:+ cookie.getName()的toString());
}
}
您可以通过这种方式从web视图中提取所有的cookie当前的URL作为字符串:
@覆盖
公共无效onPageFinished(web视图查看,字符串URL){
字符串饼干= CookieManager.getInstance()的getCookie(URL)。
Log.d(TAG,所有的字符串中的饼干:+饼干);
}
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.
The problem is, I can achieve this using android.webkit.CookieManager, and output all cookies in a single string.
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.
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
@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());
}
}
You can extract all cookies current url by this way from webview as string:
@Override
public void onPageFinished(WebView view, String url){
String cookies = CookieManager.getInstance().getCookie(url);
Log.d(TAG, "All the cookies in a string:" + cookies);
}
这篇关于安卓 - 在登录后的WebView提取饼干的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!