URLConnection sample program in Java

By: Ivan Lim Viewed: 153987 times  Printer Friendly Format    


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. We'll examine the most useful elements of URLConnection here.

In the following example, we create a URLConnection using the openConnection( ) method of a URL object and then use 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.java-samples.com/j2me/");
URLConnection hpCon = hp.openConnection();
System.out.println("Date: " + new Date(hpCon.getDate()));
System.out.println("Content-Type: " +
hpCon.getContentType());
System.out.println("Expires: " + hpCon.getExpiration());
System.out.println("Last-Modified: " +
new Date(hpCon.getLastModified()));
int len = hpCon.getContentLength();
System.out.println("Content-Length: " + len);
if (len > 0) {
System.out.println("=== Content ===");
InputStream input = hpCon.getInputStream();
int i = len;
while (((c = input.read()) != -1) && (—i > 0)) {
System.out.print((char) c);
}
input.close();
} else {
System.out.println("No Content Available");
}
}
}

The program establishes an HTTP connection to http://www.java-samples.com over port 80 
and requests the document /j2me/. We then list out the header values and retrieve the content.
The URL and URLConnection classes are good enough for simple programs that want to connect to HTTP servers to fetch content. For more complex applications, you'll probably find that you are better off studying the specification of the HTTP protocol and implementing your own wrappers.



Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. Thanks, this is exactly i am looking for. I need t
View Tutorial          By: VictorH at 2009-08-31 16:11:55

2. Thanks, this is exactly i am looking for. I need t
View Tutorial          By: Ankita at 2009-11-15 19:41:16

3. i want url full form
View Tutorial          By: arun at 2010-12-10 03:33:49

4. Very good example .. i ve written this program . P
View Tutorial          By: gomz at 2011-09-26 12:33:54

5. Nice example..i m working on android and i want to
View Tutorial          By: Pranav at 2012-04-25 07:00:48

6. how to run it on command prompt...i tried but it i
View Tutorial          By: gk at 2013-11-15 15:54:51

7. this is the output i get.
Help me.
<

View Tutorial          By: shubham saxena at 2014-04-01 17:06:00

8. Hi to all, it's in fact a nice for me to visit thh
View Tutorial          By: Barcelona Strip club at 2017-03-19 21:57:41

9. I must thank you for the efforts you have put in w
View Tutorial          By: Market Research at 2017-07-18 03:24:14

10. Brettkip
View Tutorial          By: Brettkip at 2017-07-20 17:55:23