Class of the Month: Making HTTP Connections
HTTP is the protocol that makes the Web click. It is generic enough to carry any type of data including HTML, raw data, image files, and XML. There are times when the resource your application needs is available from a Web site. You can see it by pointing your browser to its URL. You just need to have your Java application access it. The java.net.HttpURLConnection class will help you do just that. By encapsulating the HTTP protocol in a class, your application can interact with any resource that has an HTTP URL associated with it. This includes posting HTML forms to servlets and JSPs as if they were filled via a browser.
HttpURLConnection is an abstract class, so you can not directly instantiate it. When you create an instance of java.net.URL with an "http" URL and invoke its
openConnection() method, you get an instance of HttpURLConnection. This instance will be your key to sending and receiving HTTP messages both directly or through a proxy server. Here is a code fragment that will result in creation of an instance of HttpURLConnection:
URL url = new URL("http://www.developer.com"); HttpURLConnection httpcon = url.openConnection();
The HTTP protocol is based on a request/response messaging sequence. The most common requests a client makes are GET and POST requests. For example, when you type the URL
http://www.gamelan.com/index.html on your browser, the browser will send a GET message like this:
GET /index.html HTTP 1.0
HttpURLConnection by default will send GET messages. You can change that using the
setRequestMethod() method. The argument can be one of GET, POST, HEAD, PUT, OPTIONS, DELETE and TRACE. Of course, the Web server must have a complete implementation of HTTP in order to recognize and respond to all of these request types. Sometimes, you have a reverse problem in that the Web server supports additional request methods that are not supported by
|"The HttpURLConnection class is useful in cases where you need to access or interact with a resource that is already Web-enabled."|
The server will automatically terminate the connection after sending a response. If the Keep-Alive feature of HTTP is used, then a single connection can be utilized to transport multiple requests/responses. In that case, use the
disconnect() method to indicate end of connection. You can use the
getOutputStream() methods from the parent class URLConnection for reading/writing from/to the connection.
The HTTP server's response contains several parts. The first line is a numerical message indicating the status of the response. If all went okay, it should be:
HTTP 1.1 200 OK
Other parts of the response message include Date, Server software information (version, name), content length and content type. The actual message then follows, which usually is some HTML code. The first line of the response and the response code can be retrieved using
getResponseCode(), respectively. The various response codes are wrapped inside the class as fields, such as
HTTP_OK. You can refer to the HTTP specification for a detailed explanation of the various response codes.
Sometimes the response is actually a redirect to another site. You can instruct the HttpURLConnection instance whether to follow redirects or not by using the
setInstanceFollowRedirects() and passing a true or false value as argument. To set the value globally for all connections, use
setFollowRedirects() instead. You can also check to see if a particular connection is going through a Web proxy server by calling the
usingProxy() method, which returns a Boolean value.
The HttpURLConnection class is useful in cases where you need to access or interact with a resource that is already Web-enabled. You can use the class to submit searches to multiple search engines, extract information from various sites, or as a simple monitoring tool to check whether the target resource is responding in a timely manner. The class can be used without extensive knowledge of the HTTP specification, which makes it even more practical.
Do you know of a useful class and would like it to be featured as "Class of the Month?" Contact me at email@example.com.
About the Author
Piroz Mohseni is president of Bita Technologies, focusing on business improvement through the effective use of technology. His areas of interest include enterprise Java, XML, and e-commerce applications.