Jsoup网站登录抛出未知异常

时间:2016-02-11 21:04:22

标签: java jsoup

  

我尝试登录我校的成绩簿网站,使用以下代码显示HTML:

public class Connect {



public static void main(String[] args) {

    String pageURL = "https://parents.mtsd.k12.nj.us/genesis/parents?tab1=studentdata&action=form";
    String param1 = "&tab2=gradebook";
    String param2 = "&tab3=weeklysummary";
    String param3 = "&studentid=";
    Scanner input = new Scanner (System.in);
    String initURL = "https://parents.mtsd.k12.nj.us/genesis/parents?gohome=true";




    //System.out.print("Enter Student ID: ");
    //String sID = input.nextLine();
    String sID = "not shown";

    String URLF = pageURL + param1 + param2 + param3 + sID;

    connectTo(URLF);


    // write your code here
}

public static Document connectTo (String URLF){

    String loginURL = "https://parents.mtsd.k12.nj.us/genesis/parents/j_security_check";
    String userDataUrl = URLF;
    String username = "not shown";
    String password = "not shown";

    Connection.Response res = null;
    Document doc = null;

    try {

        res = Jsoup.connect(userDataUrl)
                .userAgent("Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.4 Safari/537.36")
                .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
                .timeout(500)
                .method(Connection.Method.GET)
                .execute();

    } catch (IOException io) {

        io.printStackTrace();

    }

    try {

        doc = Jsoup.connect(loginURL)
                .userAgent("Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.4 Safari/537.36")
                .referrer("https://parents.mtsd.k12.nj.us/genesis/parents?gohome=true")
                .cookies(res.cookies())
                .data("j_username", username)
                .data("j_password", password)
                .post();


    } catch (IOException ioe) {

        ioe.printStackTrace();

    }

    if (doc != null){
        System.out.print(doc.text());
    }
    return null;
}
}
  

但是,当我运行它时,我收到以下错误:

javax.net.ssl.SSLHandshakeException:  sun.security.validator.ValidatorException: PKIX path building failed:  sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1506)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:914)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
at  sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDele gateHttpsURLConnection.java:185)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:512)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:493)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:205)
at com.gs.Connect.connectTo(Connect.java:58)
at com.gs.Connect.main(Connect.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:146)
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:131)
at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
... 26 more
Exception in thread "main" java.lang.NullPointerException
at com.gs.Connect.connectTo(Connect.java:71)
at com.gs.Connect.main(Connect.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
  

现在,我不确定为什么这不起作用,因为我一直在使用这个类似的过程(使用Jsoup)来登录/解析来自类似网站的信息,而且我还没有遇到任何问题。任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:3)

execpetion unable to find valid certification path to requested target的起源的简短说明。

只有在信任证书的情况下,Java才会通过SSL建立安全连接。信任的证书存储在信任库$ {JAVA_HOME} / lib / security / cacerts中。信任库包含众所周知的证书颁发机构(CA)列表。如果远程服务器的证书既未由信任任务中的某个CA签名,也未由远程服务器本身的证书在信任中进行签名,则该连接将不受信任,并且会抛出SunCertPathBuilderException

在这种情况下,证书由GoDaddy.com签署,该your_keystore.jks不在Java信任中。因此,必须手动将服务器证书添加到默认Java信任库或单独的https://parents.mtsd.k12.nj.us版本中。

以下步骤说明如何下载服务器证书并将其存储在separat密钥库中。然后,此密钥库将用于验证服务器证书。

默认情况下不要为所有Java不受信任的连接执行此操作。在您这样做之前,必须确保您连接的服务器是您要连接的服务器。


首先,您需要下载该网页的证书。

E.g。使用Chrome浏览器(针对Linux描述的步骤,Windows类似,OSX检查下面的评论):

  • 打开网页https://
  • 点击地址栏中的锁定图标(Connection之前)
  • 在对话框中
  • 单击选项卡Certificate information
  • 点击链接Certificate Viewer
  • details点击标签Export
  • 点击按钮BASE 64 encoded single certificate并以[{1}}格式将证书保存为文件owa.mtsd.us.b64(可以是任何其他文件名)

steps 1-4 steps 5-6

接下来创建一个密钥库文件(一个命令行)

keytool -import -noprompt -trustcacerts \
    -alias owa.mtsd.us -file owa.mtsd.us.b64 \
    -keystore your_keystore.jks -storepass YourPasswd

以下代码段成功建立了连接

// without setting your created keystore
// a sun.security.validator.ValidatorException will be thrown
System.setProperty("javax.net.ssl.trustStore", "your_keystore.jks");
String url = "https://parents.mtsd.k12.nj.us";
Document doc = Jsoup.connect(url).get();
System.out.println("doc = " + doc.text());

输出

doc = One Moment...