Java中接收HTML网页时出现错误。

huangapple go评论75阅读模式
英文:

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);
    }
    
  }
} 

huangapple
  • 本文由 发表于 2020年8月19日 14:54:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63481509.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定