URL url = new URL("http://www.example.com"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); Map> headers = connection.getHeaderFields(); for (String key : headers.keySet()) { System.out.println(key + ":" + headers.get(key)); }
URL url = new URL("http://www.example.com"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); String contentType = connection.getHeaderField("Content-Type"); System.out.println("Content-Type:" + contentType);These examples demonstrate how to use the HttpURLConnection.getHeaderFields() method to retrieve different types of response headers from an HTTP URL. The HttpURLConnection class is part of the java.net package library.