Java Reference
In-Depth Information
The do - while loop in lines 32-35 reads the web document line by line, appending each
line to the StringBuffer object created to hold the page's text.
After all the data has been read, line 36 converts the string buffer into a string with the
toString() method and then puts that result in the program's text area by calling the
component's setText( String ) method.
The HttpUrlConnection class includes several methods that affect the HTTP request or
provide more information:
getHeaderField( int ) —Returns a string containing an HTTP header such as
“Server” (the web server hosting the document) or “Last-Modified” (the date the
document was last changed)
Headers are numbered from 0 upward. When the end of the headers is reached, this
method returns null
n
getHeaderFieldKey( int ) —Returns a string containing the name of the numbered
header (such as “Server” or “Last-Modified”) or null
n
getResponseCode() —Returns an integer containing the HTTP response code for
the request, such as 200 (for valid requests) or 404 (for documents that could not
be found)
n
getResponseMessage() —Returns a string containing the HTTP response code and
an explanatory message (for example: “HTTP/1.0 200 OK”)
The HttpUrlConnection class contains integer class variables for each of the valid
response codes, including HTTP_OK , HTTP_NOT_FOUND , and HTTP_MOVED_PERM
n
getContentType() —Returns a string containing the MIME type of the web docu-
ment; some possible types are text/html for web pages and text/xml for XML
files
n
setFollowRedirects( boolean ) —Determines whether URL redirection requests
should be followed ( true ) or ignored ( false ); when redirection is supported, a
URL request can be forwarded by a web server from an obsolete URL to its correct
address
n
The following code could be added to the getData() method of WebReader to display
headers along with the text of a document:
String key;
String header;
int i = 0;
do {
key = conn.getHeaderFieldKey(i);
header = conn.getHeaderField(i);
Search WWH ::




Custom Search