Program to demonstrate working of URLConnection class and the methods of the URLConnection class for Java Networking
Output of the program :
package com.hubberspot.networking.example; import java.net.*; import java.io.*; import java.util.Date; class URLConnectionExample { public static void main(String args[]) throws Exception { int ch; URL url = new URL("http://www.gmail.com"); URLConnection connection = url.openConnection(); System.out.println("Date: " + new Date(connection.getDate())); System.out.println("Content-Type: " + connection.getContentType()); System.out.println("Expires: " + connection.getExpiration()); System.out.println("Last-Modified: " + new Date(connection.getLastModified())); int len = connection.getContentLength(); System.out.println("Content-Length: " + len); if (len > 0) { System.out.println("===Print Content ==="); InputStream input = connection.getInputStream(); int i = len; while (((ch = input.read()) != -1) && (--i > 0)) { System.out.print((char) ch); } input.close(); } else { System.out.println("No Content Available"); } } }
Output of the program :