URLConnection

java.net.URLConnection Example

URLConnection class is used for accessing the attributes of a remote resource. It represents a communication link between the URL and the application.This class can be used to read and write data to resource provided by the URL.

URLConnection has subclasses HttpURLConnection which has support for HTTP-specific features and JarURLConnection which has support for JAR-specific features.

In this example we are going to read data using URLConnection.
 
 

Project Environment

This example was implemented using the following tools :

  • Eclipse 4.3 (Kepler)
  • JDK 1.8

1. Example of URLConnection

We are going to create and open URlconnection for reading data from specific URL. Create a java class named URLConnectionExample and paste the following code.

URLConnectionExample.java:

package com.javacodegeeks.snippets.URLConnection;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Date;

/**
* <p>The URLConnectionExample class</P>
* Class is used to demonstrate java.net.URLConnection example
*/
public class URLConnectionExample
{
private static final String FINAL_URL="http://www.javacodegeeks.com/";

/**
* @param args
* @throws IOException
*/
public static void main(String args[]) throws IOException
{
    StringBuilder content = new StringBuilder();
    // create a url object
    URL url = new URL(FINAL_URL);

    // create a url connection object
    URLConnection urlConnection = url.openConnection();

   //display Content Type
   System.out.println("Content-Type: " +urlConnection.getContentType());

   // display Content Length
   System.out.println("Content-Length: " + urlConnection.getContentLength());

  // display Date
  System.out.println( "Date: " +new Date(urlConnection.getDate()));

  BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
  String line;

  // read from the urlconnection via the bufferedreader
  while ((line = bufferedReader.readLine()) != null)
  {
    content.append(line + "\n");
  }
  bufferedReader.close();
  System.out.println("output:\n "+content);
}
}

Now let us explain above code:

1. Create new java URL object, passing in the required URL.
2. Use the URL object to create a URLConnection object.The openConnection() method of URL class returns the object of URLConnection class.
3. The URLConnection class contains many methods that let you communicate with the URL over the network.
4. The BufferedReader readLine method returns a string which we are appending to StringBuilder object.
For a better understanding, you can see the output of the execution of the above code.

output:

Content-Type: text/html; charset=UTF-8
Content-Length: 83058
Date: 2015-01-19
output:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
......

Download the source file

This was an example of URLConnection in java.

Download
You can download the full source code of this example here: URLConnectionExample.zip

Vaibhav Kulkarni

Vaibahv is PH.D candidate and holds masters degree in computer application. He is active contributor to Apache Software Foundation as well as openJDK.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button