gpt4 book ai didi

java - java接收html网页时出错

转载 作者:行者123 更新时间:2023-12-04 22:37:49 24 4
gpt4 key购买 nike

下面的代码用于获取 html 网页

import java.net.*;
import java.io.*;
import java.io.File; // Import the File class
import java.io.IOException; // Import the IOException class to handle errors

public class TestClass2 {

public static void main(String[] args) throws Exception {



try{
URL url = new URL("https://stackoverflow.com/");

HttpURLConnection urlConnection=(HttpURLConnection)url.openConnection();

BufferedReader reader = new BufferedReader( new InputStreamReader(url.openStream()));
String line;
while ((line = reader.readLine()) != null)
{
System.out.println(line+"\n");
}
reader.close();
}catch(Exception ex){
System.out.println(ex);
}

}
}


但是在编译和运行时会出现以下错误:
javax.net.ssl.SSLException:收到致命警报:protocol_version。
如何解决?
谢谢。

最佳答案

这可能是 SSL 证书已过期?您是否尝试过使用 HttpsURLConnection?先试试这个
修订代码

import java.net.*;
import java.io.*;
import java.io.File; // Import the File class
import java.io.IOException; // Import the IOException class to handle errors

public class TestClass2 {

public static void main(String[] args) throws Exception {



try{
URL url = new URL("https://stackoverflow.com/");

HttpsURLConnection urlConnection=(HttpsURLConnection)url.openConnection();

BufferedReader reader = new BufferedReader( new InputStreamReader(url.openStream()));
String line;
while ((line = reader.readLine()) != null)
{
System.out.println(line+"\n");
}
reader.close();
}catch(Exception ex){
System.out.println(ex);
}

}
}

关于java - java接收html网页时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63481509/

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