How the URL and URLConnection Classes Work In Java

Introduction

In this article we discuss the URL and URLConnection classes in Java.

What is URL

It takes the form of a string that describes how to find a resource on the Internet. URLs have two main components: the protocol needed to access the resource and the location of the resource. This class represents an URL. It is an acronym for Uniform Resource Locator. It points to or dertermines a resource on the World Wide Web. For example:

http://www.c-sharpcorner.com/authors/fd0172/sandeep-sharma.aspx

The following figure shows the URL. It is the web address that we have typed into a browser like http://www.google.com/.

url.jpg

An URL contains many peices of information.

Protocol
In this case, HTTP is the protocol.
Server name or IP Address
In this case, www.c-sharpcorner.com is the server name.
Port Number
It is an optional attribute, If we writehttp://www.c-sharpcorner.com:90/authors/fd0172/ , 90 is the the port number.
File name or directory name
In this case, /authors/fd0172/sandeep-sharma.aspx is the file name.

Some commonly used public methods of the URL class are:

String getProtocol
it returns the protocol of the URL.
String getHost
It returns the host name of the URL.
String getPort()
it returns the Port Number of the URL.
String getFile()
returns the file name of the URL.

Example

This example displays the Protocol, Host name, Port number and File name of a given URL:

//URLDEMO.java

import java.io.*;

import java.net.*;

public class URLEx

  {

    public static void main(String[] args)

      {

        try

          {

            URL u=new URL("http://www.c-sharpcorner.com/authors/fd0172/sandeep-sharma.aspx");

            System.out.println("Protocol: "+u.getProtocol());

            System.out.println("Port Number: "+u.getPort());

            System.out.println("File Name: "+u.getFile());

            System.out.println("Host Name: "+u.getHost());

          }

        catch(Exception ex)

          {

            System.out.println(ex);

          }

      }

  }

Output

Fig-1.jpg

URL Connection class

This class represents a communication link between the URL and the application. This class can be used to read and write data to the specified resource referred to by the URL.

Get the object of URL Connection class

The open Connection() method of URL class returns the object of URL Connection class. Syntax of following are

public URLConnection openConnection() throws IOException{}

}

Displaying all the data of a web page by URLConnection class

This class provides many methods, we can print or display all the data values of a webpage using the getInputStream() method. This method returns all the data of a specified URL in the stream that can be read and displayed.

Example

In this example all the data values of a webpage are printed using the URLConnection class:

//URLDEMO.java

import java.io.*;

import java.net.*;

public class DisplayDataEx

  {

    public static void main(String[] args)

      {

        try

          {

            URL u=new URL("http://www.c-sharpcorner.com/authors/fd0172/sandeep-sharma.aspx");

            URLConnection ucon=u.openConnection();

            InputStream s=ucon.getInputStream();

            int i;

            while((i=s.read())!=-1)

              {

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

              }

          }

        catch(Exception ex)

          {

            System.out.println(ex);

          }

      }

  }

Output

When we run our program we get all the data of our webpage; so the output shown below is not completely shown, it only gives us a reference.

Fig-2.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all