Chapter: Java The Complete Reference - The Java Library - Networking

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

URLConnection - Java Networking

URLConnection is a general-purpose class for accessing the attributes of a remote resource. Once you make a connection to a remote server, you can use URLConnection to inspect the properties of the remote object before actually transporting it locally.

URLConnection


URLConnection is a general-purpose class for accessing the attributes of a remote resource. Once you make a connection to a remote server, you can use URLConnection to inspect the properties of the remote object before actually transporting it locally. These attributes are exposed by the HTTP protocol specification and, as such, only make sense for URL objects that are using the HTTP protocol.

URLConnection defines several methods. Here is a sampling:




Notice that URLConnection defines several methods that handle header information. A header consists of pairs of keys and values represented as strings. By using getHeaderField( ), you can obtain the value associated with a header key. By calling getHeaderFields( ), you can obtain a map that contains all of the headers. Several standard header fields are available directly through methods such as getDate( ) and getContentType( ).

 

The following example creates a URLConnection using the openConnection( ) method of a URL object and then uses it to examine the document’s properties and content:

 

// Demonstrate URLConnection.

import java.net.*;

 

import java.io.*;

import java.util.Date;

 

class UCDemo

 

{

 

public static void main(String args[]) throws Exception { int c;

 

URL hp = new URL(http://www.internic.net"); URLConnection hpCon = hp.openConnection();

 

// get date

 

long d = hpCon.getDate(); if(d==0)

 

System.out.println("No date information."); else

 

System.out.println("Date: " + new Date(d));

 

// get content type

 

System.out.println("Content-Type: " + hpCon.getContentType());

 

// get expiration date

 

d = hpCon.getExpiration(); if(d==0)

 

System.out.println("No expiration information."); else

 

System.out.println("Expires: " + new Date(d));

 

// get last-modified date

 

d = hpCon.getLastModified(); if(d==0)

 

System.out.println("No last-modified information."); else

 

System.out.println("Last-Modified: " + new Date(d));

 

// get content length

 

long len = hpCon.getContentLengthLong(); if(len == -1)

 

System.out.println("Content length unavailable."); else

 

System.out.println("Content-Length: " + len);

 

if(len != 0) {

 

System.out.println("=== Content ===");

 

InputStream input = hpCon.getInputStream(); while (((c = input.read()) != -1)) {

System.out.print((char) c);

 

}

input.close();

 

} else {

 

System.out.println("No content available.");

 

}

 

}

 

}

 

The program establishes an HTTP connection to www.internic.net over port 80. It then displays several header values and retrieves the content. You might find it interesting to try this example, observing the results, and then for comparison purposes try a different web site of your own choosing.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.