Jsoup网站登录抛出未知异常
Jsoup website login throwing unknown exception
I attempt to login to my school's grade book website to display the HTML with the following code:
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;
}
}
However, when I run this, I get the following error:
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)
Now, I am not sure why this is not working as I have been using this similar process (with Jsoup) to login/parse information from similar websites and I haven't run into any issues. Any help is appreciated!
对执行起源的简短解释 unable to find valid certification path to requested target
。
Java 只有在信任证书时才会通过 SSL 建立安全连接。信任的证书存储在信任库 ${JAVA_HOME}/lib/security/cacerts 中。 trustore 包含众所周知的证书颁发机构 (CA) 的列表。如果远程服务器的证书既不是由 trustore 中的一个 CA 签名的,也不是远程服务器本身的证书在 trustore 中,则连接将不被信任并抛出 SunCertPathBuilderException
。
在这种情况下,证书是由 GoDaddy.com
签署的,它不在 Java 信任库中。因此必须在默认 Java trustore 或单独的 your_keystore.jks
中手动添加服务器证书。
以下步骤说明了如何下载服务器证书并将其存储在单独的密钥库中。然后,此密钥库将用于验证服务器证书。
默认情况下不这样做 所有 Java 不受信任的连接。在您这样做之前,您 必须确保您连接的服务器是您要连接的服务器。
首先,您需要下载该网页的证书。
例如使用 Chrome 浏览器(针对 Linux、Windows 描述的步骤类似,OSX 查看下面的评论):
- 打开网页
https://parents.mtsd.k12.nj.us
- 单击地址栏中的锁定图标(
https://
之前)
- 在对话框中单击选项卡
Connection
- 点击link
Certificate information
- 在
Certificate Viewer
中单击选项卡 details
- 单击按钮
Export
并以 BASE 64 encoded single certificate
格式将证书保存为文件 owa.mtsd.us.b64
(可以是任何其他文件名)
接下来创建一个密钥库文件(一个命令行)
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...
I attempt to login to my school's grade book website to display the HTML with the following code:
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;
}
}
However, when I run this, I get the following error:
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)
Now, I am not sure why this is not working as I have been using this similar process (with Jsoup) to login/parse information from similar websites and I haven't run into any issues. Any help is appreciated!
对执行起源的简短解释 unable to find valid certification path to requested target
。
Java 只有在信任证书时才会通过 SSL 建立安全连接。信任的证书存储在信任库 ${JAVA_HOME}/lib/security/cacerts 中。 trustore 包含众所周知的证书颁发机构 (CA) 的列表。如果远程服务器的证书既不是由 trustore 中的一个 CA 签名的,也不是远程服务器本身的证书在 trustore 中,则连接将不被信任并抛出 SunCertPathBuilderException
。
在这种情况下,证书是由 GoDaddy.com
签署的,它不在 Java 信任库中。因此必须在默认 Java trustore 或单独的 your_keystore.jks
中手动添加服务器证书。
以下步骤说明了如何下载服务器证书并将其存储在单独的密钥库中。然后,此密钥库将用于验证服务器证书。
默认情况下不这样做 所有 Java 不受信任的连接。在您这样做之前,您 必须确保您连接的服务器是您要连接的服务器。
首先,您需要下载该网页的证书。
例如使用 Chrome 浏览器(针对 Linux、Windows 描述的步骤类似,OSX 查看下面的评论):
- 打开网页
https://parents.mtsd.k12.nj.us
- 单击地址栏中的锁定图标(
https://
之前) - 在对话框中单击选项卡
Connection
- 点击link
Certificate information
- 在
Certificate Viewer
中单击选项卡details
- 单击按钮
Export
并以BASE 64 encoded single certificate
格式将证书保存为文件owa.mtsd.us.b64
(可以是任何其他文件名)
接下来创建一个密钥库文件(一个命令行)
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...