英文:
Error in receiving a html webpage by java
问题
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);
}
}
}
Error:
javax.net.ssl.SSLException: Received fatal alert: protocol_version.
Question:
How can I fix it?
Thanks.
英文:
below code is for getting html web page
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);
}
}
}
but when compile and run that below error occur:
javax.net.ssl.SSLException: Received fatal alert: protocol_version.
how can fix it?
thanks.
答案1
得分: 0
这可能是由于SSL证书过期了?你尝试过使用HttpsURLConnection了吗?首先尝试这个。
修订后的代码:
import java.net.*;
import java.io.*;
import java.io.File; // 导入File类
import java.io.IOException; // 导入IOException类以处理错误
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(urlConnection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line + "\n");
}
reader.close();
} catch (Exception ex) {
System.out.println(ex);
}
}
}
英文:
This could be that the SSL Certificate is out of date? Have you tried using HttpsURLConnection? Try this first
Revised Code
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);
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论