URL and URLConnection Classes In Java

Introduction

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

What is the 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 determines a resource on the World Wide Web. For example:
 
 
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 pieces 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 write http://www.c-sharpcorner.com:90/authors/fd0172/, 90 is 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 hostname 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:
  1. //URLDEMO.java  
  2. import java.io.*;  
  3. import java.net.*;  
  4. public class URLEx  
  5. {  
  6.     public static void main(String[] args)  
  7.     {  
  8.         try  
  9.         {  
  10.             URL u = new URL("http://www.c-sharpcorner.com/authors/fd0172/sandeep-sharma.aspx");  
  11.             System.out.println("Protocol: " + u.getProtocol());  
  12.             System.out.println("Port Number: " + u.getPort());  
  13.             System.out.println("File Name: " + u.getFile());  
  14.             System.out.println("Host Name: " + u.getHost());  
  15.         } catch (Exception ex)  
  16.         {  
  17.             System.out.println(ex);  
  18.         }  
  19.     }  
  20. }  
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 the following are
  1. 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:
  1. //URLDEMO.java  
  2. import java.io.*;  
  3. import java.net.*;  
  4. public class DisplayDataEx  
  5. {  
  6.     public static void main(String[] args)  
  7.     {  
  8.         try  
  9.         {  
  10.             URL u = new URL("http://www.c-sharpcorner.com/authors/fd0172/sandeep-sharma.aspx");  
  11.             URLConnection ucon = u.openConnection();  
  12.             InputStream s = ucon.getInputStream();  
  13.             int i;  
  14.             while ((i = s.read()) != -1)  
  15.             {  
  16.                 System.out.print((char) i);  
  17.             }  
  18.         } catch (Exception ex)  
  19.         {  
  20.             System.out.println(ex);  
  21.         }  
  22.     }  
  23. }  
Output
 
When we run our program we get all the data on our webpage; so the output shown below is not completely shown, it only gives us a reference.
 
Fig-2.jpg