Home Java Network How to retrieve URL information



How to retrieve URL information
Posted on: December 18, 2008 at 12:00 AM
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.

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.
 

Related Tags for How to retrieve URL information:
curlclassormformnetworkobjectiomakemethodformataiforieexampleworktoexaminformationeusetrieinrminfoasmtrnetnetjclretrieveallmeobjexplainxaxampsetwatkinfllivmplgoandarxptwssrithbeloatiinforminformatjepleplplaindono


More Tutorials from this section

Ask Questions?    Discuss: How to retrieve URL information   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.