当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Jsoup HTTPS connecting

问题描述:

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.

网友答案:

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());
            }
        });
分享给朋友:
您可能感兴趣的文章:
随机阅读: