Error in receiving a html webpage by java

Viewed 54

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 Answers

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