File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Sockets and Internet Protocols and the fly likes Problem with getHeaderField() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Problem with getHeaderField()" Watch "Problem with getHeaderField()" New topic

Problem with getHeaderField()

Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 884

The following code seems to work correctly because I can retrieve the contents of the URL ok, but I don't get any results from the getHeaderField() call. Any ideas?

URL url = new URL( urlstr );
HttpURLConnection con = (HttpURLConnection)
con.setRequestMethod( "GET" );
con.setDoInput( true );
con.setDoOutput( false );
// display header fields
for( int i=0 ; ; i++ )
String key;
if( (key = con.getHeaderFieldKey(i)) ==
null )
System.out.println( key + " = "
+ con.getHeaderField(i) );
// etc.

Understanding the scope of the problem is the first step on the path to true panic
srinivas madugula

Joined: Apr 12, 2003
Posts: 6
the indexing begins at 1 not 0.
getHeaderFieldKey(0) returns null
getHeaderField(0) returns the http response
HTTP/1.0 status some_info_regarding status.
getHeaderFieldKey(1..) returns header key
getHeaderField(1..) returns header value
your program stops on i=0.
start with i=1;
it will work
I agree. Here's the link:
subject: Problem with getHeaderField()
It's not a secret anymore!