我想在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");
请告诉我我的代码有什么问题?
答案 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");