[ACCEPTED]-jsoup posting and cookie-jsoup

Accepted answer
Score: 111

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")

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)
Score: 19
//This will get you the response.
Response res = Jsoup
    .data("loginField", "login@login.com", "passField", "pass1234")

//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")


Score: 1

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.

Score: 0

Here is what you can try...

import org.jsoup.Connection;

Connection.Response res = null;
    try {
        res = Jsoup
                .data("username", "your login id", "password", "your password")
    } catch (IOException e) {

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){

Ask if further help needed.

Score: 0
Connection.Response res = Jsoup.connect("http://www.example.com/login.php")
    .data("username", "myUsername")
    .data("password", "myPassword")
//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")
//This gives the data of the required website


Score: 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) {
                } catch (IOException ioe) {

More Related questions