package org.kodejava.net;
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:
null = HTTP/1.1 200 OK,
Referrer-Policy = no-referrer-when-downgrade,
Content-Length = 147566,
Content-Type = text/html; charset=UTF-8,
Connection = keep-alive,
Date = Sun, 26 Sep 2021 03:59:06 GMT,
Accept-Ranges = bytes,
Vary = Accept-Encoding, Accept-Encoding,
Link = <https://wp.me/8avgG>; rel=shortlink, <https://kodejava.org/wp-json/>; rel="https://api.w.org/",
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024