URLConnection

Get response headers from HTTP request

With this example we are going to demonstrate how to get response headers from an HTTP request in Java. In short, to get the response headers from an HTTP request you should :

  • Create a URL Object that represents the resource you want to access
  • Use the openConnection() API method of the URL Object to access connection specific parameters for the HTTP request
  • Use the getHeaderFields() API method from the connection Object to get a Name-Value pare Map Object containing all respose headers for the specific HTTP request

You can Iterate over the contents of the header’s Map Object as shown in the code snippet below.

package com.javacodegeeks.snippets.core;

import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class GetResponseHeadersFromHTTPRequest {
	
	public static void main(String[] args) throws Exception {
		
		URL url = new URL("http://www.google.com:80");
		URLConnection conn = url.openConnection();

		Map<String, List<String>> headerFields = conn.getHeaderFields();

		Set<String> headerFieldsSet = headerFields.keySet();
		Iterator<String> hearerFieldsIter = headerFieldsSet.iterator();
		
		while (hearerFieldsIter.hasNext()) {
			
			 String headerFieldKey = hearerFieldsIter.next();
			 List<String> headerFieldValue = headerFields.get(headerFieldKey);
			 
			 StringBuilder sb = new StringBuilder();
			 for (String value : headerFieldValue) {
				 sb.append(value);
				 sb.append("");
			}
			 
			 System.out.println(headerFieldKey + "=" + sb.toString());
			
		}
		
	}

}

Output:

null=HTTP/1.1 200 OK
X-Frame-Options=SAMEORIGIN
Date=Sat, 29 Oct 2011 19:59:40 GMT
Transfer-Encoding=chunked
Expires=-1
X-XSS-Protection=1; mode=block
Set-Cookie=NID=52=k77gRcAV5E3Gh7PIobW26tSHsyc2gBeTEOHastZBxQdvqJ5JwfFCbhEaJh8uQ0bH3PFNvnpeGGtumB8pe1XWMcF7dQC9TugHOgzseBEl6CxOTmcvq_RHGwdQcFq6LUZQ; expires=Sun, 29-Apr-2012 19:59:40 GMT; path=/; domain=.google.gr; HttpOnlyPREF=ID=a372139a705e35a1:FF=0:TM=1319918380:LM=1319918380:S=i8JHod81lVWzINZg; expires=Mon, 28-Oct-2013 19:59:40 GMT; path=/; domain=.google.gr
Content-Type=text/html; charset=ISO-8859-7
Server=gws
Cache-Control=private, max-age=0

This was an example of how to get response headers from an HTTP request in Java.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button