Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,970 questions

51,912 answers

573 users

How to read HTTPS (HTTP over SSL) URLs in Java

1 Answer

0 votes
package javaapplication1;

import java.net.MalformedURLException;
import java.net.URL;
import java.io.*;
import javax.net.ssl.HttpsURLConnection;

public class JavaApplication1 {

    public static void main(String[] args) {

        System.out.println(new JavaApplication1().https_connect("https://en.wikipedia.org/wiki/HTTPS"));
    }

    private String https_connect(String https_url) {

        URL url;

        try {
            url = new URL(https_url);
            HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
            con.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");

            return get_content(con);

        } catch (MalformedURLException e) {
            System.out.println(e.getMessage());
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return "Error";
    }

    private String get_content(HttpsURLConnection con) {

        String html = "";
        if (con != null) {

            try {

                BufferedReader br = new BufferedReader(
                        new InputStreamReader(con.getInputStream()));

                String line;

                while ((line = br.readLine()) != null) {
                    html = html + "\r\n" + line;
                }
                br.close();

            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
        return html;
    }
}

/*
run:

<!DOCTYPE html>
<html lang="en" dir="ltr" class="client-nojs">
<head>
<meta charset="UTF-8"/>
<title>HTTPS - Wikipedia, the free encyclopedia</title>
<script>document.documentElement.className = document.documentElement.className...

...

*/

 



answered Jun 27, 2016 by avibootz

Related questions

1 answer 265 views
2 answers 188 views
1 answer 127 views
1 answer 128 views
1 answer 127 views
...