URLConnection sample program in Java

By: Ivan Lim  

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.




Archived Comments

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

2. I must thank you for the efforts you have put in writing this blog.
I am hoping to see the sa

View Tutorial          By: Market Research at 2017-07-18 03:24:14

3. Hi to all, it's in fact a nice for me to visit thhis site, it includes precious Information.
View Tutorial          By: Barcelona Strip club at 2017-03-19 21:57:41

4. this is the output i get.
Help me.

Date: Tue Apr 01 22:32:41 IST 2014
C

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

5. how to run it on command prompt...i tried but it is giving url exception..
View Tutorial          By: gk at 2013-11-15 15:54:51

6. Nice example..i m working on android and i want to sync my android application with my website DB ca
View Tutorial          By: Pranav at 2012-04-25 07:00:48

7. Very good example .. i ve written this program . Please help me.

public static void m

View Tutorial          By: gomz at 2011-09-26 12:33:54

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

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

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


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial