Java如何从HTTP请求获取响应标头?
package org.nhooo.example.network;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
public class HttpResponseHeaderDemo {
public static void main(String[] args) {
try {
URL url = new URL("https://nhooo.com/index.php");
URLConnection connection = url.openConnection();
Map<String, List<String>> responseMap = connection.getHeaderFields();
for (String key : responseMap.keySet()) {
System.out.print(key + " = ");
List<String> values = responseMap.get(key);
for (String value : values) {
System.out.print(value + ", ");
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}上面的代码产生的结果是:
Transfer-Encoding = chunked, Keep-Alive = timeout=5, max=100, null = HTTP/1.1 200 OK, Server = Apache, Connection = Keep-Alive, Link = <https://nhooo.com/wp-json/>; rel="https://api.w.org/", <https://wp.me/8avgG>; rel=shortlink, Date = Mon, 07 May 2018 07:48:34 GMT, Content-Type = text/html; charset=UTF-8,
热门推荐
1 狗狗技能训练
5 如何训练狗狗笑
6 狗狗训练排便
9 狗狗响片训练
10 如何训练狗狗不捡食
11 狗狗尿训练
12 怎么训练狗狗找东西
13 狗狗训练站立
14 狗狗训练飞盘
15 鞍山狗狗训练
16 怎样训练泰迪狗狗谢谢
17 狗狗外出训练
18 怎么训练金毛狗狗大小便