Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

HttpURLConnection headerFieldKey is not set correctly

807580Jun 24 2010 — edited Jun 28 2010
Hi,

I have the following situation, where the same piece of code, returns the expected header value, i.e. "text/html;charset=UTF-8" when run against one URL and returns another header value, i.e. "text/html; charset=ISO-8859-1" when run against a different URL. The relevant sections of my code are -
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setInstanceFollowRedirects(false);
conn.connect();

for (int i=0; ; i++) {
        String headerName = conn.getHeaderFieldKey(i);	            
        String headerValue = conn.getHeaderField(i);           
    
        System.out.println("headerName: " + headerName + "; headerValue: " + headerValue);

         if (headerName == null && headerValue == null) {
              break;
          }
}
I need the value of the "Content-Type" headerName to be set to "text/html;charset=UTF-8", which is true for one URL automatically, but not for the other. The URLs are internal so they won't be relevant if I post here - but they are essentially the same.

I have tried to change the value associated with the "Content-Type" headerName as follows -
conn.setRequestProperty ("Content-Type",  "application/xml;charset=UTF-8");
But that has no effect at all.

Am I missing something subtle here, or may be I am trying to do it the wrong way? Please help!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 26 2010
Added on Jun 24 2010
7 comments
291 views