URLConnection.getHeaderFields() has the following syntax.
public Map < String , List < String >> getHeaderFields()
In the following code shows how to use URLConnection.getHeaderFields() method.
import java.net.URL; import java.net.URLConnection; import java.util.List; import java.util.Map; import java.util.Set; /*from w ww .j ava 2 s . c om*/ public class Main { public static void main(String[] args) throws Exception { URL url = new URL("http://www.java2s.com/"); URLConnection urlConnection = url.openConnection(); Map<String, List<String>> headers = urlConnection.getHeaderFields(); Set<Map.Entry<String, List<String>>> entrySet = headers.entrySet(); for (Map.Entry<String, List<String>> entry : entrySet) { String headerName = entry.getKey(); System.out.println("Header Name:" + headerName); List<String> headerValues = entry.getValue(); for (String value : headerValues) { System.out.print("Header value:" + value); } System.out.println(); } } }
The code above generates the following result.