This article demonstrates how to obtain HTTP response header information through urlconnection
1. Obtain header information from the response
URL obj = new URL("http://www.qiyadeng.com"); URLConnection conn = obj.openConnection(); Map<String, List<String>> map = conn.getHeaderFields();
2. Obtain 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.qiyadeng.com"); urlconnection conn = obj. openconnection (); Map <string, list <string> map = Conn. getheaderfields (); system. out. println ("display response header information \ n"); For (map. entry <Str Ing, list <string> entry: map. entryset () {system. out. println ("key:" + entry. getkey () + ", value:" + entry. getvalue ();} system. out. println ("\ n get response header information using key \ 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) ;}} catch (exception e) {e. printstacktrace ();}}}
Output
Display response header information...
Key: NULL, value: [http/1.1 200 OK] key: X-pingback, value: [http://www.qiyadeng.com/xmlrpc.php?key: date, value: [Sun, 10 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)
Original article, reprinted Please note:Reprinted from http://www.qiyadeng.com/
Link:Java obtains HTTP Response Header Information