Jsoup HTTPS connecting

匿名 (未验证) 提交于 2019-12-03 01:26:01

问题:

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.

回答1:

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());             }         }); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!