Following code segment prints the complete header of Java Forums site.
String str = "http://www.java-forums.org";
try {
URL u = new URL(str);
URLConnection uc = u.openConnection( );
for (int j = 1; ; j++) {
String header = uc.getHeaderField(j);
if (header == null) break;
System.out.println(uc.getHeaderFieldKey(j) + ": " + header);
} // end for
} // end try
catch (MalformedURLException ex) {
System.err.println(str + " is not a URL I understand.");
}
catch (IOException ex) {
System.err.println(ex);
}
System.out.println( );
Output:
Date: Mon, 26 Nov 2007 12:25:57 GMT
Server: Apache/1.3.39 (Unix) PHP/4.4.7 mod_auth_passthrough/1.8
mod_log_bytes/1.2 mod_bwlimited/1.4 mod_ssl/2.8.30 OpenSSL/0.9.8b
X-Powered-By: PHP/4.4.7
Set-Cookie: bbsessionhash=9504ff3cde4b9d424d1bf362769a7c17;
path=/; HttpOnly
Set-Cookie: bblastvisit=1196079957; expires=Tue, 25 Nov 2008 12:25:57 GMT; path=/
Set-Cookie: bblastactivity=0; expires=Tue, 25 Nov 2008 12:25:57 GMT; path=/
Cache-Control: private
Pragma: private
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=ISO-8859-1