Java Tutorial - Java Script : Communicating Across the Internet

Java Tutorial - Java Script :

Communicating Across the Internet

·         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
·         getHeaderFieldKey(int)—Returns a string containing the name of the numbered header (such as “Server” or “Last-Modified”) or null
·         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)
·         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
·         getContentType()—Returns a string containing the MIME type of the web document; some possible types are text/html for web pages and text/xml for XML files
·         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
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);
458 DAY 17: Communicating Across the Internet
if (key == null) {
key = “”;
} else {
key = key + “: “;
}
if (header != null) {
text.append(key + header + “\n”);
}
i++;
} while (header != null);
text.append(“\n”);