I would like to login into a website and keep the cookies. I have issues with the login part using Jsoup;
My code ;
Connection.Response loginForm = Jsoup.connect("URL" + "/login/").cookies(cookies).method(Connection.Method.GET).execute();
formData.put("username", "#######");
formData.put("pwd", "########");
formData.put("hidden","69");
formData.put("token", loginForm.parse().select("input#token").first().attr("value"));
Connection.Response homePage = Jsoup.connect("URL" + "/login/")
.referrer("URL" + "/login/")
.followRedirects(true)
.cookies(cookies)
.data(formData)
.method(Connection.Method.POST)
.execute();
cookies_2.putAll(homePage.cookies()); // save the cookies, this will be passed on to next request
If i go to the login page and use the developper tool there are ;
Edit 1;
The problem now is I get homepage of the website but without the section of login, I keep the "Connect or register" button.
I output the cookies_2 and it is the same that the cookies in the Chrome logs ;
Now, what I don't understand is why, I don't get loggin if I have the right cookies ?
Edit 2;
I chanded my code with the final solution and it work !
Thanks for your help !
Few remarks:
Jsoup.connect(...).followRedirects(true)
Jsoup.connect(...).referrer(URL + "/login")
hidden
value is always 69
? Maybe it's different for each request. You can get it dynamically like this: formData.put("hidden",html.select("co_js").first().attr("value"));
I don't like your way of getting the token. Let's use Jsoup to extract it:
String authToken = html.select("input#token").first().attr("value");
Edit:
Jsoup.connect(...).header("Content-Type","application/x-www-form-urlencoded")
I got the idea by analyzing headers in Chrome's dev tools. Now I can successfully login.Document doc = homePage.parse();
System.out.println("Logged in as: " + doc.select(".dropdown-toggle").text());
By letting server know you can handle compressed pages you can decrease downloaded page size. For every request use: Jsoup.connect(...).header("accept-encoding", "gzip, deflate")
It's transparent and you don't have to do anything special to handle it, but it works internally.
Edit 2:
Providing final solution based on previous advices:
import java.util.HashMap;
import java.util.Map;
import org.jsoup.Connection;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class Stackoverflow51734840 {
private static final String URL = "https://time2watch.in";
private static final String URL_LOGIN = URL + "/login/";
static String userAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36";
public static void main(final String[] args) throws Exception {
Map<String, String> headers = new HashMap<String, String>();
headers.put("accept-encoding", "gzip, deflate");
Connection.Response loginForm = Jsoup.connect(URL + "/login").userAgent(userAgent).headers(headers).execute();
Map<String, String> cookies = loginForm.cookies();
Document html = loginForm.parse();
String authToken = html.select("input#token").first().attr("value");
System.out.println("Found authToken:" + authToken);
Map<String, String> formData = new HashMap<String, String>();
formData.put("username", "!!!!!!!!!!!!!!!!!!");
formData.put("pwd", "!!!!!!!!!!!!!!!!!!");
formData.put("hidden", "69");
formData.put("token", authToken);
headers.put("Content-Type", "application/x-www-form-urlencoded");
System.out.println("cookies before login:");
System.out.println(cookies);
System.out.println(" Logged in cookie present? " + cookies.containsKey("s4icookuser"));
Connection.Response afterLoginPage = Jsoup.connect(URL_LOGIN).cookies(cookies).headers(headers)
.userAgent(userAgent).data(formData).method(Connection.Method.POST).referrer(URL_LOGIN).execute();
// update cookies
cookies = afterLoginPage.cookies();
System.out.println("cookies after login:");
System.out.println(cookies);
System.out.println(" Logged in cookie present? " + cookies.containsKey("s4icookuser"));
Response homePage = Jsoup.connect(URL).cookies(cookies).method(Connection.Method.GET).userAgent(userAgent)
.referrer(URL_LOGIN).followRedirects(true).referrer(URL_LOGIN).headers(headers).execute();
Document doc = homePage.parse();
System.out.println("Error? " + doc.text().contains("Erreur"));
System.out.println("OK? " + !doc.text().contains("Se connecter"));
System.out.println("Logged in as: " + doc.select(".dropdown-toggle").text());
}
}