如何在java中获取响应头

时间:2013-10-04 07:39:42

标签: java

我想在java中获取响应头。

URL obj = new URL("http://mkyong.com");

    //get all headers
    Map<String, List<String>> map = conn.getHeaderFields();
    for (Map.Entry<String, List<String>> entry : map.entrySet()) {
        System.out.println("Key : " + entry.getKey() + 
                 " ,Value : " + entry.getValue());
    }

    //get header by 'key'
    String server = conn.getHeaderField("Server");

请告诉我我的代码有什么问题?

1 个答案:

答案 0 :(得分:1)

URL obj = new URL("http://mkyong.com");
    URLConnection conn = obj.openConnection();

    //get all headers
    Map<String, List<String>> map = conn.getHeaderFields();
    for (Map.Entry<String, List<String>> entry : map.entrySet()) {
        System.out.println("Key : " + entry.getKey() + 
                 " ,Value : " + entry.getValue());
    }

    //get header by 'key'
    String server = conn.getHeaderField("Server");