Jsoup HTTPS连接 [英] Jsoup HTTPS connecting

查看:267
本文介绍了Jsoup HTTPS连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图登录一个网站,保存cookie并在提供cookie的同时连接到另一个站点,然后继续抓取该网站.全部使用Jsoup和常规Java(我打算稍后再使用Android).我的问题是,带有loginform的网站使用的是https,并且出现大量错误.搜索后,我发现了这个stackoverflow问题.描述的方法涉及从登录站点获取SSL证书并将扩展名从.crt更改为.jks.但是,我不断收到错误,因此无法使其正常工作.这是我的代码

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

那么,有人知道如何允许Jsoup建立https连接吗?如前所述,我想将应用程序移植"到Android,在这种情况下,我认为不可能使用静态证书(?).

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(?).

谢谢.

推荐答案

尝试以下操作(只需将其放在Jsoup.connect("之前https://login.emu.dk ):

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

这篇关于Jsoup HTTPS连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆