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 220 views
1 answer 296 views
3 answers 265 views
1 answer 206 views
2 answers 215 views
1 answer 148 views
...