Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Network How to retrieve URL information

Related Tutorials


 
 

Share on Google+Share on Google+

How to retrieve URL information

Advertisement
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.
 

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 18, 2008

Related Tutorials

Discuss: How to retrieve URL information   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com