Java Reference
In-Depth Information
for ( int j = 1 ; ; j ++) {
String header = uc . getHeaderField ( j );
String key = uc . getHeaderFieldKey ( j );
if ( header == null || key == null ) break ;
System . out . println ( uc . getHeaderFieldKey ( j ) + ": " + header );
}
System . out . println ();
try ( InputStream in = new BufferedInputStream ( uc . getInputStream ())) {
// chain the InputStream to a Reader
Reader r = new InputStreamReader ( in );
int c ;
while (( c = r . read ()) != - 1 ) {
System . out . print (( char ) c );
}
}
} catch ( MalformedURLException ex ) {
System . err . println ( args [ 0 ] + " is not a parseable URL" );
} catch ( IOException ex ) {
System . err . println ( ex );
}
}
}
}
The only thing this program doesn't read that the server sends is the version of HTTP
the server is using. There's currently no method to specifically return that. In this ex‐
ample, you just fake it as “HTTP/1.x,” like this:
% java SourceViewer3 http://www.oreilly.com
HTTP/1.x 200 OK
Date: Sat, 04 May 2013 11:59:52 GMT
Server: Apache
Last-Modified: Sat, 04 May 2013 11:41:06 GMT
Accept-Ranges: bytes
Content-Length: 80165
Content-Type: text/html; charset=utf-8
Cache-Control: max-age=14400
Expires: Sat, 04 May 2013 15:59:52 GMT
Vary: Accept-Encoding
Keep-Alive: timeout=3, max=100
Connection: Keep-Alive
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
...
However, uc.getHeaderField(0) does return the entire first HTTP request line, ver‐
sion included:
HTTP/1.1 200 OK
Search WWH ::




Custom Search