Using class in Networking

The class establishes a link between your Java program and a resource on the network, a URL. Here is a program on how we can get various details regarding a URL such as it's content type, the last modified date etc.

import java.util.*;
import java.text.*;
class Connect
    public static void main(String args[])

    // The url may not well formed, So MalformedURLException

    // URL pointing to
    URL u=new URL("");

    // IOException might occur, when the URL is not

available/readable etc.

    // Create URLConnection object
    URLConnection con=u.openConnection();

    // Connect to the url

    // Print the url
    System.out.println("URL: "+con.getURL());

    // Print the type of content
    System.out.println("Content type: "+con.getContentType());

    // Get date (long)
    long d=con.getDate();

    // Create date object for above value
    Date date=new Date(d);

    // Create SimpleDateFormat for above Date object
    SimpleDateFormat sd=new SimpleDateFormat("dd/MM/YYYY");
    // Print the date
    System.out.println("Date: "+sd.format(date));

    // Get the last modified date
    date=new Date(con.getLastModified());

    // Print the last modified date
    System.out.println("Last Modified: "+sd.format(date));
    // Print the content of URL (returns Object)
    System.out.println("Content: "+con.getContent());

    }catch(IOException e){
        System.out.println("Unable to connect to the url");

    }catch(MalformedURLException e){
    System.out.println("The given url is not well formed.");



Content type: text/html; charset=UTF-8
Date: 06/04/2013
Last Modified: 06/04/2013


The constructor of used here takes a String which is the url of the resource. Here it is a.k.a

In order to get the details of the resource we need a URLConnection object here. So it is created using openConnection() method in the URL class.

The connect() method is used to interact with the resource. Note that, the above works (i.e. the program's output is one and the same) even when you don't call this connect() method.

Classes and their presence 

URL, URLConnection, MalformedURLException classes are from package.
Date class is from java.util package.
SimpleDateFormat is from java.text package.
IOException is from package.

Exception clauses

The first exception clause is for the URL constructor. This might thrown an exception called the MalformedURLException. The name itself says that it occurs when the URL is not correctly formed. For example htp: instead of http: etc. This try clause is the outer clause.

The inner try clause is for the openConnection() method which might throw an exception which is nothing but IOException. You might be familiar with this. This exception occurs when the resource is unable to read/write. Here, the URL points to the resource ( If the index.html file here is unable to be read, then this exception occurs. It is obvious that this might occur when there is no internet connection. The inner clause is declared here because if there is no URL object i.e. if u is null, then there will be no URLConnection object since u is null and openConnection() cannot be called.

Posted by at on

Tags: Networking,