<style type="text/css">
p, li, td, ul { font-family: Arial, Helvetica, sans-serif}
Java provides a subclass of URLConnection that provides support for HTTP connections.
This class is called HttpURLConnection. You obtain an HttpURLConnection in the same
way just shown, by calling openConnection( ) on a URL object, but you must cast the result
to HttpURLConnection. (Of course, you must make sure that you are actually opening an
HTTP connection.) Once you have obtained a reference to an HttpURLConnection object,
you can use any of the methods inherited from URLConnection. You can also use any of the
several methods defined by HttpURLConnection. Here is a sampling:
static boolean getFollowRedirects( )
Returns true if redirects are automatically followed and
false other wise. This feature is on by default.
String getRequestMethod( )
Returns a string representing how URL requests are
made. The default is GET. Other options, such as POST,
are available.
int getResponseCode( )
Returns the HTTP response code. 1 is returned if no
throws IOException
response code can be obtained. An IOException is
thrown if the connection fails.
String getResponseMessage( )
Returns the response message associated with the
throws IOException
response code. Returns null if no message is available.
An IOException is thrown if the connection fails.
static void
If how is true, then redirects are automatically followed.
setFollowRedirects(boolean how)
If how is false, redirects are not automatically followed.
By default, redirects are automatically followed.
void setRequestMethod(String how)
Sets the method by which HTTP requests are made to
that specified by how. The default method is GET, but
throws ProtocolException
other options, such as POST, are available. If how is
invalid, a ProtocolException is thrown.
The following program demonstrates HttpURLConnection. It first establishes a
connection to Then it displays the request method, the response code,
and the response message. Finally, it displays the keys and values in the response header.
// Demonstrate HttpURLConnection.
import java.util.*;
class HttpURLDemo
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks