2

我想在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");

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

4

1 回答 1

2
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");
于 2013-10-04T07:41:06.360 回答