package org.kodejava.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://kodejava.org/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();
}
}
}
The result produced by the code above are:
Transfer-Encoding = chunked,
Keep-Alive = timeout=5, max=100,
null = HTTP/1.1 200 OK,
Server = Apache,
Connection = Keep-Alive,
Link = <https://kodejava.org/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,
Wayan Saryada
Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. If these posts help, you can support me, buy me a cup of coffee or tea. Thank you 🥳
Latest posts by Wayan Saryada (see all)
- How do I set the time of java.util.Date instance to 00:00:00? - October 24, 2019
- How to Install Consolas Font in Mac OS X? - March 29, 2019
- How do I clear the current command line in terminal? - February 14, 2019