java - java接收html网页时出错
问题描述
下面的代码用于获取 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);
}
}
}
推荐阅读
- .net-core - 登录时在启动类中注册服务
- android - React-Native (Android) - 任务 ':app:transformClassesWithMultidexlistForRelease' 执行失败
- angular - 使用迷你图标变体从末端位置打开 mat-sidenav
- javascript - 我的 WordPress 网站的特定页面上的页面加载时间缓慢的问题
- angular - 无法格式化角度时间戳
- c++ - OpenGL:mat4x4 与 vec4 相乘得到 tvec
- macos - 在 Mac 上调试 IE11
- c# - Entity Framework Core LINQ 麻烦创建(选择案例存在)查询
- c# - 将笔记本电脑连接到多台显示器时出现显示器编号问题
- python - 具有 3 个不同选项的按钮