Get HTTP response header information via URLConnection in Java

Source: Internet
Author: User
Tags header centos

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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.