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

Shoshi picture Shoshi · Feb 17, 2013 · Viewed 7.8k times · Source

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?

Answer

Shoshi picture Shoshi · Feb 18, 2013

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.