3

How to read the https page content using java??

Rahul
  • 259
  • 1
  • 7
  • 15

1 Answers1

2

Following is an example of retrieving the content of the page for "https://maven.apache.org/guides/mini/guide-repository-ssl.html" It works for any page that have https protocol only if a login is NOT required. Just replace the httpsURL string. Enjoy!

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

/**
 * <b>Get the Html source from a https url </b>
 */
public class HttpsClientUtil {
    public static void main(String[] args) throws Exception {
        String httpsURL = "https://maven.apache.org/guides/mini/guide-repository-ssl.html";

        URL myurl = new URL(httpsURL);
        HttpsURLConnection con = (HttpsURLConnection) myurl.openConnection();
        con.setRequestProperty ( "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0" );
        InputStream ins = con.getInputStream();
        InputStreamReader isr = new InputStreamReader(ins);
        BufferedReader in = new BufferedReader(isr);
        String inputLine;

        while ((inputLine = in.readLine()) != null) {
            System.out.println(inputLine);
        }
        in.close();
    }
}
Supercoder
  • 1,066
  • 1
  • 10
  • 16