| Article Index |
|---|
| 1. The Application |
| 2. Sample Output |
This 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.
1. The Application
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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);
}
}
}
1 |
package testit; |
2. Sample Output
The following listing provides an example how the application ist working.
# run with default configuration (http)
$ java testit.HttpsTest
connecting to: http://www.linux-support.com/test/test.txt
connection 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.txt
Received https://www.linux-support.com/test/test.txt
Received
# run with optional parameter (https)
$ java testit.HttpsTest https://secure.wikimedia.org/ | head -7
connecting 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:




