gpt4 book ai didi

java - Jsoup网站登录抛出未知异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:39:46 24 4
gpt4 key购买 nike

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!

最佳答案

执行源的简短说明无法找到到所请求目标的有效证书路径

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

在这种情况下,证书由不在 Java trustore 中的 GoDaddy.com 签名。因此必须在默认 Java trustore 或单独的 your_keystore.jks 中手动添加服务器证书。

以下步骤解释了如何下载服务器证书并将其存储在单独的 keystore 中。然后,此 keystore 将用于验证服务器证书。

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


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

例如使用 Chrome 浏览器(针对 Linux 描述的步骤,Windows 类似,OSX 检查下面的注释):

  • 打开网页https://parents.mtsd.k12.nj.us
  • 点击地址栏中的锁图标(在 https:// 之前)
  • 在对话框中单击选项卡Connection
  • 点击链接证书信息
  • Certificate Viewer 中单击选项卡 details
  • 单击按钮导出 并将证书以BASE 64 编码的单一证书 格式保存为文件owa.mtsd.us.b64 (可以是任何其他文件名)

steps 1-4 steps 5-6

接下来创建一个keystore文件(一个命令行)

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...

关于java - Jsoup网站登录抛出未知异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35349980/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com