import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Test{
public static void main(String[] args) throws IOException{
URL url = new URL("http://www.baidu.com");
URLConnection conn = url.openConnection();
Map<String, List<String>> headers = conn.getHeaderFields();
Set<String> keys = headers.keySet();
for( String key : keys ){
String val = conn.getHeaderField(key);
System.out.println(key+" "+val);
}
}
}
运行结果为:
Accept-Ranges bytes
null HTTP/1.1 200 OK
Cache-Control private, no-cache, no-store, proxy-revalidate, no-transform
Server bfe/1.0.8.18
ETag "588604e2-94d"
Connection Keep-Alive
Set-Cookie BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
Pragma no-cache
Last-Modified Mon, 23 Jan 2017 13:28:02 GMT
Content-Length 2381
Date Mon, 19 Jun 2017 00:56:33 GMT
Content-Type text/html
end