| Refresh | Home EGTry.com

get http response headers


get response header

package urlconnection;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

public class ResponseHeader {

	
	public static void main(String[] args) throws Exception {
		URL link=new URL("http://www.google.com");
		URLConnection conn=link.openConnection();
		Map<String, List<String>> headers=conn.getHeaderFields();
		for(String header: headers.keySet()) {
			List<String> values=headers.get(header);
			for(String val: values) {
				System.out.println(header+" => "+val);
			}
		}
		
	}

}


response headers sample output

null => HTTP/1.1 200 OK
Date => Wed, 20 Apr 2011 03:41:39 GMT
Transfer-Encoding => chunked
Expires => -1
X-XSS-Protection => 1; mode=block
Set-Cookie => NID=46=Qrqv_NnjV9seJCqzS_WT1uE5qqBt8hHMBF9JqtxcmryCHtnLcA8xab03QK6Fjfxd9bYwQR7gRk2JfZvaIwZ5iFkX6uxe8x0z_W_tcdKmJHCoLBAYWRfx-t3ipwKv3IAj; expires=Thu, 20-Oct-2011 03:41:39 GMT; path=/; domain=.google.com; HttpOnly
Set-Cookie => PREF=ID=6056bb82b3d99db8:FF=0:TM=1303270899:LM=1303270899:S=PeEy6DrdNGZC-YrH; expires=Fri, 19-Apr-2013 03:41:39 GMT; path=/; domain=.google.com
Content-Type => text/html; charset=ISO-8859-1
Server => gws
Cache-Control => private, max-age=0