Reading a Web Page via HTTP


URL url = new URL("http://www.timothyfisher.com"); HttpURLConnection http = new HttpURLConnection(url); InputStream in = http.getInputStream();



In this phrase, we go beyond socket level network programming and show you an additional way of reading data from a network. Java supports communication with a URL over HTTP with the HttpURLConnection class. We instantiate a URL instance by passing a valid URL string to the URL constructor. We then instantiate an HttpURLConnection by passing the url instance into the HttpURLConnection constructor. The getInputStream() method is called to get an input stream for reading data from the URL connection. Using the input stream, we could then read the contents of the web page.

You can also read the contents of a URL using the URL class directly. Here is an example of how we do this using only the URL class:

URL url = new URL("http://www.timothyfisher.com"); url.getContent();


The getContent() method returns an Object. The object returned can be an InputStream or an object containing the data. A common example would be for the getContent() method to return a String object containing the contents of a URL. The getContent() method that we used here is actually shorthand for the following code:

url.openConnection.getContent();


The openConnection() method of the URL class returns an URLConnection object. This is the object that the getContent() method is actually implemented in.

The HttpURLConnection provides HTTP specific methods not available in the more general URL or URLConnection classes. For example, we can use the getresponseCode() method to get the status code from an HTTP response message. HTTP also defines a protocol for redirecting a request to a different server. The HttpURLConnection class has methods that understand this feature as well. For example, if you want to make a request to a server and follow any redirects that the server returns, you can use the following code to set this option:

URL url = new URL("http://www.timothyfisher.com"); HttpURLConnection http = new HttpURLConnection(url); http.setFollowRedircts(true);


This option is actually set to be true by default, so a more useful scenario might be setting the follow redirects option to false if, for some reason, you do not want to be automatically redirected to a server other than the one that you initially made the request on. For example, you might want to consider this for certain security applications from which you only trust specified servers.

Web pages that contain sensitive data are usually protected by a security protocol called Secure Sockets Layer, commonly referred to as SSL. An SSL protected page is referred to using https in the URL string, as opposed to http. The standard Java JDK does include an implementation of SSL as part of the Java Secure Socket Extension (JSSE). In order to retrieve an SSL protected page, you would use the HttpsURLConnection class instead of the HttpURLConnection class. The HttpsURLConnection class transparently handles all of the details of the SSL protocol for you. For more details on using SSL and other security features provided by JSSE, see the JSSE reference guide provided by Sun at: http://java.sun.com/j2se/1.5.0/docs/guide/security/jsse/JSSERefGuide.html.




JavaT Phrasebook. Essential Code and Commands
Java Phrasebook
ISBN: 0672329077
EAN: 2147483647
Year: 2004
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net