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 HTTP and HTTPS (HTTP over SSL) URLs through arguments (args) in Java

1 Answer

0 votes
package javaapplication1;
 
import java.net.MalformedURLException;
import java.net.URL;
import java.io.*;
import java.net.HttpURLConnection;
import javax.net.ssl.HttpsURLConnection;
 
public class JavaApplication1 {
 
    public static void main(String[] args) {
 
        if (args[0].toLowerCase().contains("https:"))
            System.out.println(new JavaApplication1().https_connect(args[0]));
        else
            System.out.println(new JavaApplication1().http_connect(args[0]));
    }
 
    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_https_content(con);
 
        } catch (MalformedURLException e) {
            System.out.println(e.getMessage());
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return "Error";
    }
 
    private String get_https_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;
    }
    
    private String http_connect(String http_url) {
 
        URL url;
 
        try {
            url = new URL(http_url);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
 
            return get_http_content(con);
 
        } catch (MalformedURLException e) {
            System.out.println(e.getMessage());
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return "Error";
    }
    private String get_http_content(HttpURLConnection 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:

 
*/

 



answered Sep 14, 2016 by avibootz

Related questions

1 answer 200 views
1 answer 265 views
3 answers 235 views
1 answer 182 views
2 answers 188 views
1 answer 127 views
...