[ACCEPTED]-jsoup posting and cookie-jsoup
Accepted answer
When you login to the site, it is probably 5 setting an authorised session cookie that 4 needs to be sent on subsequent requests 3 to maintain the session.
You can get the 2 cookie like this:
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
And then send it on the 1 next request like:
Document doc2 = Jsoup.connect("http://www.example.com/otherPage")
.cookie("SESSIONID", sessionId)
.get();
//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();
0
Where the code was:
Document doc = Jsoup.connect("urlYouNeedToBeLoggedInToAccess").cookies().get();
I was having difficulties 2 until I changed it to:
Document doc = Jsoup.connect("urlYouNeedToBeLoggedInToAccess").cookies(cookies).get();
Now it is working 1 flawlessly.
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 3 your cookies and make request to the other 2 page you want.
//Store Cookies
cookies = res.cookies();
Making request to another 1 page.
try {
Document doc = Jsoup.connect("your-second-page-link").cookies(cookies).get();
}
catch(Exception e){
e.printStackTrace();
}
Ask if further help needed.
Connection.Response res = Jsoup.connect("http://www.example.com/login.php")
.data("username", "myUsername")
.data("password", "myPassword")
.method(Connection.Method.POST)
.execute();
//Connecting to the server with login details
Document doc = res.parse();
//This will give the redirected file
Map<String,String> cooki=res.cookies();
//This gives the cookies stored into cooki
Document docs= Jsoup.connect("http://www.example.com/otherPage")
.cookies(cooki)
.get();
//This gives the data of the required website
0
Why reconnect? if there are any cookies 1 to avoid 403 Status i do so.
Document doc = null;
int statusCode = -1;
String statusMessage = null;
String strHTML = null;
try {
// connect one time.
Connection con = Jsoup.connect(urlString);
// get response.
Connection.Response res = con.execute();
// get cookies
Map<String, String> loginCookies = res.cookies();
// print cookie content and status message
if (loginCookies != null) {
for (Map.Entry<String, String> entry : loginCookies.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue().toString() + "\n");
}
}
statusCode = res.statusCode();
statusMessage = res.statusMessage();
System.out.print("Status CODE\n" + statusCode + "\n\n");
System.out.print("Status Message\n" + statusMessage + "\n\n");
// set login cookies to connection here
con.cookies(loginCookies).userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0");
// now do whatever you want, get document for example
doc = con.get();
// get HTML
strHTML = doc.head().html();
} catch (org.jsoup.HttpStatusException hse) {
hse.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.