Reputation: 153
So, I'm trying to log into a site, save the cookies and connect to another site while providing the cookies and proceed to scrape the website. All using Jsoup and regular java (I plan to move to Android later on). My problem is, that the site with the loginform is using https and there I am getting a ton of errors. After searching around I found this stackoverflow question. The method described involving getting the SSL certificate from the login-site and change the extension from .crt to .jks. However, I keep getting the errors and therefore unable to make it work. Here's my code
public static void main(String args[]) throws IOException {
//Log in
Connection.Response login = Jsoup.connect("https://login.emu.dk")
.data("login", "myUsername")
.data("pass", "myPassword")
.method(Method.POST)
.execute();
//Keep logged in
Map<String,String> loginCookies = login.cookies();
ArrayList<Lesson> unsorted = new ArrayList<Lesson>();
URL elevplan = new URL("https://elevplan.dk");
CSVParser csvParser = new CSVParser();
System.setProperty("javax.net.ssl.trustStore", "/Users/philipjakobsen/Desktop/login.emu.dk.jks");
System.setProperty("javax.net.ssl.trustStore", "/Users/philipjakobsen/Desktop/login.emu2.dk.jks");
Document doc = Jsoup.connect("https://elevplan.dk")
.cookies(loginCookies)
.get();
So, does anyone know how to allow Jsoup to make https connections? As earlier said, I would like to "port" my application to Android, and in this scenario I don't think it is possible to use a static certificate(?).
Thanks in advance.
Upvotes: 0
Views: 5374
Reputation: 3599
Try following (just put it before Jsoup.connect("https://login.emu.dk"):
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password.toCharArray());
}
});
Upvotes: 3