my dog learned polymorphism
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

Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
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: 765

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.

When I was your age I was toggling in the bootstrap code on the front panel
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!