View Javadoc

1   package com.sri.emo.auth;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   import java.net.HttpURLConnection;
6   import java.net.URL;
7   import java.net.UnknownHostException;
8   
9   /***
10   * This is a sample client that can use grab the contents of a page.  The single command line
11   * argument is the URL to retrieve.  The result is sent to standard out.
12   * @author Michael Rimov
13   *
14   */
15  public class SSLClient {
16  	
17  	/***
18  	 * URL to connect to.  Should include full SCHEME (http/https) and port.
19  	 */
20  	private String url;
21  	
22  
23  
24  	/***
25  	 * Main method.
26  	 * @throws UnknownHostException
27  	 * @throws IOException
28  	 */
29  	public void run() throws UnknownHostException, IOException {
30          
31          URL url = new URL(getUrl());        
32          HttpURLConnection connection = (HttpURLConnection)url.openConnection();
33          
34          
35          InputStream is = connection.getInputStream();
36          try {
37          int oneChar;
38          while ((oneChar = is.read()) != -1) {
39          	System.out.print((char)oneChar);
40          }
41          } finally {
42          	is.close();
43          }
44          connection.disconnect();
45  	}
46  
47  
48  	/***
49  	 * Retrieves the URL to connect.
50  	 * @return
51  	 */
52  	protected String getUrl() {
53  		assert url != null;
54  		assert url.length() > 0;
55  		
56  		return url;
57  	}
58  
59  	/***
60  	 * Sets the URL to connect to.
61  	 * @param url
62  	 */
63  	protected void setUrl(String url) {
64  		this.url = url;
65  	}
66  	
67  	
68  	/***
69  	 * Runs the program.
70  	 * @param args  The first argument should be the URL to connect to.
71  	 */
72  	public static void main(String[] args) throws Exception {
73  		SSLClient sslClient = new SSLClient();
74  		if (args.length == 0) {
75  			usage();
76  			System.exit(-1);
77  		}
78  		
79  		sslClient.setUrl(args[0]);
80  		sslClient.run();
81  	}
82  
83  
84  	private static void usage() {
85  		System.out.println("Usage: SSLClient  [url]");
86  	}
87  
88  }