以下实例演示了如何获取指定 url 的响应头信息:
/*
author by w3cschool.cc
main.java
*/import java.io.ioexception;import java.net.url;import java.net.urlconnection;import java.util.map;import java.util.set;public class main {
public static void main(string[] args) throws ioexception{
url url = new url("http://www.w3cschool.cc");
urlconnection conn = url.openconnection();
map headers = conn.getheaderfields();
set<string> keys = headers.keyset();
for( string key : keys ){
string val = conn.getheaderfield(key);
system.out.println(key+" "+val);
}
system.out.println( conn.getlastmodified() );
}}
以上代码运行输出结果为:
transfer-encoding chunked
null http/1.1 200 ok
server tengine/1.3.0
connection keep-alive
vary cookie
date mon, 04 may 2015 03:54:05 gmt
x-pingback
x-powered-by php/5.3.15
content-type text/html; charset=utf-8
以上就是java 实例 - 获取 url 响应头信息的内容。