This article shows how to get the HTTP response header information through URLConnection
1. Get header information from the response
URL obj = new URL ("http://www.bianceng.cn");
URLConnection conn = Obj.openconnection ();
map<string, list<string>> map = Conn.getheaderfields ();
2. Get the server information from the response header
map<string, list<string>> map = Conn.getheaderfields ();
list<string> Server = map.get ("server");
Complete example
Package com.qiyadeng.http;
Import Java.net.URL;
Import java.net.URLConnection;
Import java.util.List;
Import Java.util.Map;
public class Gethttpresponseheader {public static void main (string[] args) {try {
URL obj = new URL ("http://www.bianceng.cn");
URLConnection conn = Obj.openconnection ();
map<string, list<string>> map = Conn.getheaderfields ();
SYSTEM.OUT.PRINTLN ("Show response header information ... \ n"); For (map.entry<string, list<string>> entry:map.entrySet ()) {System.out.println ("Key
: "+ entry.getkey () +", Value: "+ entry.getvalue ());
System.out.println ("\ n use key to get response header information \ n");
list<string> Server = map.get ("server"); if (server = = null) {System.out.println ("KeY ' Server ' is not found! ');}
else {for (String values:server) {System.out.println (values);
A catch (Exception e) {e.printstacktrace ()); }
}
}
Output
Show Response header information ...
Key:null, value: [http/1.1 OK]
key:x-pingback, value: [http://www.bianceng.cn/xmlrpc.php]
key:date, Va Lue: [Sun, Mar 2013 12:16:26 GMT]
key:transfer-encoding, value: [chunked]
key:connection, value: [Close]
Key:content-type, Value: [text/html; Charset=utf-8]
Key:server, Value: [apache/2.2.3 (CentOS)]
key:x-powered-by, value: [php/5.2.17]
use Key to get response header information ...
apache/2.2.3 (CentOS)
Source: http://www.qiyadeng.com/