public static Map < String , List < String > > getResponseHeaders ( String stringUrl , boolean followRedirects ) throws IOException { URL url = new URL ( stringUrl ) ; HttpURLConnection conn = ( HttpURLConnection ) url . openConnection ( ) ; conn . setInstanceFollowRedirects ( followRedirects ) ; InputStream is = conn . getInputStream ( ) ; if ( "gzip" . equals ( conn . getContentEncoding ( ) ) ) { is = new GZIPInputStream ( is ) ; } Map < String , List < String > > headers = new LinkedHashMap < String , List < String > > ( conn . getHeaderFields ( ) ) ; headers . put ( "X-Content" , Arrays . asList ( MyStreamUtils . readContent ( is ) ) ) ; headers . put ( "X-URL" , Arrays . asList ( conn . getURL ( ) . toString ( ) ) ) ; headers . put ( "X-Status" , Arrays . asList ( String . valueOf ( conn . getResponseCode ( ) ) ) ) ; return headers ; }
Get the response headers for URL following redirects