*/
public class HttpURLConnectionExample {
private final String USER_AGENT = "Mozilla/5.0";
public static void main(String[] args) throws Exception {
HttpURLConnectionExample http = new HttpURLConnectionExample();
// Sending get request
http.sendingGetRequest();
}
// HTTP GET request
private void sendingGetRequest() throws Exception {
String urlString = "https://www.java2blog.com";
URL url = new URL(urlString);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
// Get all header fields
Map<String, List> map = con.getHeaderFields();
System.out.println("Printing Response Header for URL: " + url.toString() + "n");
for (Map.Entry> entry : map.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
// get specific field
String date = con.getHeaderField("Date");
System.out.println("ngetting Date from response : " + date);
// get specific field
String server = con.getHeaderField("Server");
System.out.println("ngetting Server from response : " + server);
}
}