Overview of Networking through JAVA,How to retrieve URL information

This page discusses - Overview of Networking through JAVA,How to retrieve URL information

Overview of Networking through JAVA,How to retrieve URL information

How to retrieve URL information

     

Here we are going to explain a method for retrieve all the network information for a given URL. In the given below example we use the URL class and make a object. After creating a object we assign a new URL to it. Then we just call a getProtocol() method to retrieve the protocol, getPort() method for retrieve the port number of the URL, getHost to get the host name of that URL. Apart from all of this we can also find out the file name if we pass any file name with URL by getFile() and by toExternalForm() method is use to retreive the full name of the URL.

The given example shows all the above method in proper manner. The code of the example is as under

Here is the Code of the Example :

URLDemo.java

import  java.net.*;

public class URLDemo{
  
  public static void main(String args[])throws MalformedURLException{
  URL hp  = new  URL("http://www.javajazzup.com");
  System.out.println("Protocal:" +  hp.getProtocol());
  System.out.println("Port:" +  hp.getPort());
  System.out.println("Host:" +  hp.getHost());
  System.out.println("File:" +  hp.getFile());
  System.out.println("Ext:" + hp.toExternalForm());
  }
}

The out put of the above example is as under, where the output display all the information related with the given URL.

Here is the Output of the Example :

C:\roseindia>javac URLDemo.java

C:\roseindia>java URLDemo
Protocal:http
Port:-1
Host:www.javajazzup.com
File:
Ext:http://www.javajazzup.com

Download of this example.