Log in

Registration

Testing HTTP / HTTPs Urls with Java

Posted: August 20, 2010 / in: Scripts / No comments

source-java-iconThis recipe provides a Java application to test urls by utilizing protocols supported by your Java Virtual Machine.

The following program provides the following features:

  • by default the following url is connected: http://www.linux-support.com/test/test.txt
  • by adding a url as an optional parameter you are able to connect http and https services
  • just trusted https services are supported
  • received contents are printed to stdout

If you are interested in connection HTTPs services signed by unofficial CAs or self-signed certificates you should take a look at article Custom SSL Certificates and HTTPs with Java Clients.

The Application

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
package testit; import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL; import javax.net.ssl.HttpsURLConnection;import javax.net.ssl.SSLSocketFactory; /** Connect to a HTTP or HTTPS server (runs with Java 5 and Java 6). * @author marioscondo * * Copyright 2010, Linux-Support.com */public class HttpsTest {     /**     * @param args provide an optional url     */    public static void main(String[] args) {        // main method - this is the entry point for running this application        HttpsTest app = new HttpsTest();        try {            // fetch a url from command line or use a default one            String url = "http://www.linux-support.com/test/test.txt";            if (args.length > 0) {                url = args[0];            }             System.out.println("connecting to: " + url);             // fetch remote data            app.doit(url);         } catch (Exception e) {            e.printStackTrace();        }    }     // connect to the server    void doit(String url_str) throws Exception {        InputStream inputstream = null;        if (url_str.startsWith("http://")) {            URL url = new URL(url_str);            HttpURLConnection conn = (HttpURLConnection) url.openConnection();            inputstream = conn.getInputStream();        } else {            SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();            URL url = new URL(url_str);            HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();            conn.setSSLSocketFactory(sslsocketfactory);            inputstream = conn.getInputStream();        }         InputStreamReader inputstreamreader = new InputStreamReader(inputstream);        BufferedReader bufferedreader = new BufferedReader(inputstreamreader);         String string = null;        System.out.println("connection was successful.");        System.out.println("received data:");        while ((string = bufferedreader.readLine()) != null) {            System.out.println("Received " + string);        }     }} 

 

Sample Output

The following listing provides an example how the application ist working.

# run with default configuration (http)$ java testit.HttpsTestconnecting to: http://www.linux-support.com/test/test.txtconnection was successful.received data:Received This is a text message provided for testing Received purposes by http(s)://www.linux-support.com.Received Received Data location:Received   http://www.linux-support.com/test/test.txtReceived   https://www.linux-support.com/test/test.txtReceived  # run with optional parameter (https)$ java testit.HttpsTest https://secure.wikimedia.org/ | head -7connecting to: https://secure.wikimedia.org/connection was successful.received data:Received <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">Received <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">Received  <head>Received   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

 

Related articles:

Incoming search terms:

© Copyrights and Licenses, 2012 - Linux-Support.com The Professional Linux and OSS Services Portal