how to get all cookies or cookie's url from android.webkit.CookieManager

雨燕双飞 提交于 2019-11-28 01:56:18

问题


mainly, i have loged into facebook using webview. so, i don't know which cookies for which urls are saved into CookieManager. i don't know whether it is possible or not, but i have know idea how to do it.

now, i need to get a page using Jsoup. but i need to pass some cookie also to get the page, otherwise server will return me an error pager.

i want it with Jsoup, because i need some information from the page

i have trying something like this, but all the time i am getting the error page:

Map<String, String> cookies = new HashMap<String, String>();
cookies.put(domain1, my_cookie1);
cookies.put(domain2, my_cookie2);
cookies.put(domain3, my_cookie3);
cookies.put(domain4, my_cookie4);

Document doc = Jsoup.connect(uri.toString())
                            .cookies(cookies)
                            .timeout(10000)
                            .get();
Log.e("title", doc.title());

my_cookie's are get from CookieManager. and they are not null, because i have printed them.

i think the problem is with cookies. or not? is there any solution. i think i am missing some cookie from CookieManager.

i need to get the page.

Edited:

or, is it possible to pass the cookimanager to Jsoup ? so, that it can take the cookie from cookiemanager directly. or, can i know which cookies are needed for getting my desire page?


回答1:


i have solve the problem. first of all i was getting the right cookie all time. so what was the problem then. either i was wrong to integrate the cookie with Jsoup or Jsoup was doing something wrong. so, first i have get the page with HttpUrlConnection and then parse it with Jsoup. like this:

URL form = new URL(uri.toString());
HttpUrlConnection connection1 = (HttpURLConnection)form.openConnection();
connection1.setRequestProperty("Cookie", my_cookie);
connection1.setReadTimeout(10000);
StringBuilder whole = new StringBuilder();
BufferedReader in = new BufferedReader(
new InputStreamReader(new BufferedInputStream(connection1.getInputStream())));
String inputLine;
while ((inputLine = in.readLine()) != null)
      whole.append(inputLine);
in.close();
Document doc = Jsoup.parse(whole.toString());

any advice about this code would be appreciated.



来源:https://stackoverflow.com/questions/14925061/how-to-get-all-cookies-or-cookies-url-from-android-webkit-cookiemanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!