Using java.net.URLConnection class in Networking

The java.net.URLConnection 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.net.*;
import java.io.*;
import java.util.*;
import java.text.*;
class Connect
{
    public static void main(String args[])
    {

    // The url may not well formed, So MalformedURLException
    try
    {

    // URL pointing to java-demos.blogspot.com
    URL u=new URL("http://java-demos.blogspot.com");


    // IOException might occur, when the URL is not

available/readable etc.
    try
    {

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

    // Connect to the url
    con.connect();

    // 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.");
    }

    }
}

Output


URL: http://java-demos.blogspot.com
Content type: text/html; charset=UTF-8
Date: 06/04/2013
Last Modified: 06/04/2013
Content: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@10bedb4

Notes

The constructor of java.net.URL used here takes a String which is the url of the resource. Here it is http://java-demos.blogspot.com a.k.a http://java-demos.blogspot.com/index.html

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 java.net package.
Date class is from java.util package.
SimpleDateFormat is from java.text package.
IOException is from java.io 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 (http://java-demos.blogspot.com/index.html). 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.

No comments: