Java Reference
In-Depth Information
You can also specify a URL. For example, to run this recipe with the URL of
http://www.httprecipes.com/ , you would use the following command:
IsHTTPS http://www.httprecipes.com
The above command simply shows the abstract format to call this recipe, with the ap-
propriate parameters. For exact information on how to run this recipe refer to Appendix B, C,
or D, depending on the operating system you are using. This program begins by obtaining a
URL. The URL is either provided from the command line, or defaults to a page on the HTTP
Recipes site. The following lines of code do this.
String strURL = "";
// Obtain a URL to use.
if (args.length < 1)
{
strURL = "https://www.httprecipes.com/1/5/secure/";
} else
{
strURL = args[0];
}
Next, a URL object is created for the specified URL. A connection is then opened with a
call to the openConnection function on the URL object.
URL url;
try
{
url = new URL(strURL);
URLConnection conn = url.openConnection();
Once the connection URL has been obtained, a connection is made. This will throw an
IOException if the website is not accepting connections.
conn.connect();
Next, the program will check to see what type of object was returned. If an
HttpsURLConnection object is returned, the connection is using HTTPS. If an
HttpURLConnection object is returned, the connection is using HTTP. If it is neither
of these two, then the program reports that it does not know what sort of a connection is be-
ing used.
// See what type of object was returned.
if (conn instanceof HttpsURLConnection)
{
System.out.println("Valid HTTPS URL");
} else if (conn instanceof HttpURLConnection)
{
System.out.println("Valid HTTP URL");
} else
Search WWH ::




Custom Search