Jsoup message and cookie

I try to use jsoup to enter the site and then clear the information, I have a problem, I can log in and create a document from index.php, but I cannot get other pages on the site. I know that I need to set a cookie after posting, and then load it when I try to open another page on the site. But how do I do this? The following code allows me to log in and get index.php

Document doc = Jsoup.connect("http://www.example.com/login.php") .data("username", "myUsername", "password", "myPassword") .post(); 

I know I can use apache httpclient for this, but I don't want to do this.

+47
java jsoup screen-scraping
Jun 21 2018-11-11T00:
source share
4 answers

When you log in, it may be a configured authorized session cookie, which must be sent in subsequent requests to support the session.

You can get a cookie as follows:

 Connection.Response res = Jsoup.connect("http://www.example.com/login.php") .data("username", "myUsername", "password", "myPassword") .method(Method.POST) .execute(); Document doc = res.parse(); String sessionId = res.cookie("SESSIONID"); // you will need to check what the right cookie name is 

Then send it at the following request, for example:

 Document doc2 = Jsoup.connect("http://www.example.com/otherPage") .cookie("SESSIONID", sessionId) .get(); 
+98
Jun 25 '11 at 9:16
source share
 //This will get you the response. Response res = Jsoup .connect("loginPageUrl") .data("loginField", "login@login.com", "passField", "pass1234") .method(Method.POST) .execute(); //This will get you cookies Map<String, String> loginCookies = res.cookies(); //And this is the easiest way I've found to remain in session Document doc = Jsoup.connect("urlYouNeedToBeLoggedInToAccess") .cookies(loginCookies) .get(); 
+18
May 10 '12 at 11:53
source share

Where is the code:

 Document doc = Jsoup.connect("urlYouNeedToBeLoggedInToAccess").cookies().get(); 

I had difficulties until I changed it to:

 Document doc = Jsoup.connect("urlYouNeedToBeLoggedInToAccess").cookies(cookies).get(); 

Now it works flawlessly.

+1
Dec 29 '12 at 1:14
source share

Here is what you can try ...

 import org.jsoup.Connection; Connection.Response res = null; try { res = Jsoup .connect("http://www.example.com/login.php") .data("username", "your login id", "password", "your password") .method(Connection.Method.POST) .execute(); } catch (IOException e) { e.printStackTrace(); } 

Now save all your cookies and request another page you want.

 //Store Cookies cookies = res.cookies(); 

Fulfilling a request to another page.

 try { Document doc = Jsoup.connect("your-second-page-link").cookies(cookies).get(); } catch(Exception e){ e.printStackTrace(); } 

Ask if additional help is needed.

0
Dec 05 '17 at 8:11
source share



All Articles