Posted By:
18 Feb, 2014 9:39 am PST

To retrieve IP address of a website and different parts of URL

Write a program to accept a website name and return its IP Address, after checking it on Internet.

Note: This program should be executed on a system which is connected to Internet.

//Knowing IPAddress of a website

import java.io.*;

import java.net.*;

public class Address {

 public static void main(String[] args) throws IOException{

 //accept name of website from keyboard

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

 System.out.print("Enter a website name: ");

String site=br.readLine();

try{

 //getByName() method accepts site name and returns its IPAddress

InetAddress ip= InetAddress.getByName(site);

System.out.println("The IP Address is: "+ ip);

}

catch(UnknownHostException ue){

 System.out.println("Website not found");

}

}

 }

 OUTPUT:

Write a program to retrieve different parts of a URL supplied to URL class object.

//URL

import java.net.*;

public class MyURL {

public static void main(String[] args) throws Exception {

URL ur=new URL("http://www.fixoncloud.com/Home/Helper_Webpages/AllRecentProblems.php");

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

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

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

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

System.out.println("Path: "+ ur.getFile());

System.out.println("External Form: "+ ur.toExternalForm());

}

 }

OUTPUT:


Note: Please note that port number is displayed as -1 in the output, which represents that no port number is passed in the URL.

 



Video is posted for better understanding of the concept.

Was it useful?  




Please login to reply to this problem.



Related Problems on Fixoncloud