从cookiesyncmanager Jsoup cookie认证从HTTPS站点刮 [英] Jsoup cookie authentication from cookiesyncmanager to scrape from https site

查看:354
本文介绍了从cookiesyncmanager Jsoup cookie认证从HTTPS站点刮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用上的用户被重定向到的页面我想刮的数据了与jsoup之前,用户名和密码来登录web视图Android应用程序。由于jsoup线程将是一个不同的会话用户必须重新登录。

现在我想用从web视图收到该cookie发送与jsoup要求能够刮我的数据。

该Cookie同步到cookiesyncmanager具有以下code。这基本上是在那里我坚持,因为我不知道如何读出该cookie也不知道如何将它连接到jsoup请求。请帮忙? :)

 公共无效onPageFinished(web视图查看,字符串URL){

            CookieSyncManager.getInstance()同步()。
 

在jsoup刮用户后我做了登录的是这样的:

  DOC = Jsoup.connect(https://need.authentication.com)获得();

                元素元素= doc.select(span.tabCount);
              元素数量= elements.first();


                Log.d(TAG,测试+(计数));
 

解决方案

我的没有的Andr​​oid开发人员,但也许你可以尝试这样的:

 最后的字符串URL =htt​​ps://need.authentication.com;


//  - 这里的Andr​​oid饼干部分 - 
CookieSyncManager.getInstance()同步()。
CookieManager厘米= CookieManager.getInstance();

字符串的cookie = cm.getCookie(URL); //返回的cookie的URL

// ...

//  -  JSoup一部分在这里 - 
// Jsoup使用cookies为名/值对
DOC = Jsoup.connect(https://need.authentication.com).cookie(URL,饼干)获得();

// ...
 

我希望这有助于一点,但正如我以前说过:IM没有Android开发的

(和code未经测试!)

下面是一些文档:

I have an android application using a webview on which the user has to log in with username and password before being redirected to the page i would like to scrape data off with jsoup. Since the jsoup thread would be a different session the user would have to login again.

Now i would like to use the cookie received from the webview to send with the jsoup request to be able to scrape my data.

The cookie is being synced with cookiesyncmanager with following code. This is basically where I am stuck cause i dont know how to read out the cookie nor how to attach it to the jsoup request. Please help? :)

public void onPageFinished(WebView view, String url) {

            CookieSyncManager.getInstance().sync();

The jsoup scrape I am doing after the user has logged in with something like this:

  doc = Jsoup.connect("https://need.authentication.com").get();

                Elements elements = doc.select("span.tabCount");
              Element count = elements.first();


                Log.d(TAG, "test"+(count));

解决方案

I'm not an android developer but maybe you can try something like this:

final String url = "https://need.authentication.com";


// -- Android Cookie part here --
CookieSyncManager.getInstance().sync();
CookieManager cm = CookieManager.getInstance();

String cookie = cm.getCookie(url); // returns cookie for url

// ...

// -- JSoup part here --
// Jsoup uses cookies as "name/value pairs"
doc = Jsoup.connect("https://need.authentication.com").cookie(url, cookie).get();

// ...

I hope this helps a bit, but as i said before: im no android developer (and code isn't tested!)

Here's some documentation:

这篇关于从cookiesyncmanager Jsoup cookie认证从HTTPS站点刮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆