Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Knowing webpage status code and resolving a given address using Java

In this article you will see how to create a program which can let you resolve an ip address or a host name via Java.

This program can let you resolve an ip address or a host name via Java. You can also check status code of a given site and even message telling what that status code meant.

This can be helpful in situation where you have a handful of sites and you want to check which sites are actually accessible or want to resolve their address or host name. First we will import the classes.

Listing 1: Importing classes

import java.net.InetAddress;
import org.jsoup.Jsoup;
import org.jsoup.Connection.Response;

Here,

  1. InetAddress provides methods used to resolve addresses
  2. JSoup is for connecting to webpage
  3. Response is used for storing the response which includes the status code

Now, we will define the class along with main method:

Listing 2: Class declaration along with Main method

public class WebPageStatus {

	public static void main(String args[])
	{
		String siteToResolve="www.google.com";
		String siteStatus="http://www.google.com";
		new WebPageStatus().resolve(siteToResolve);
		new WebPageStatus().status(siteStatus);
	}

Here,

  1. Name of the class is WebPageStatus
  2. We define a variable holding site whose status need to be known and the site for which we want to determine the ip address
  3. We call the resolve method to resolve the site passed
  4. We call the status method to determine the status of the site passed.

Now we define the resolve method

Listing 3: Resolve method

public void resolve(String site)
	{
		try
		{
		InetAddress addr = InetAddress.getByName(site);
		System.out.println("Host Name:"+addr.getHostName());
		System.out.println("Host Address:"+addr.getHostAddress());
		}
		catch(Exception e)
		{
			System.out.println("Unable to Resolve");
		}
	}

Here,

  1. Here we pass the site to the InetAddress object.
  2. Now we use the getHostName() method to obtain the host name
  3. we use the getHostAddress() method to obtain the host address
  4. Now we define the status method to obtain status of a given webpage:

    Listing 4:Status method

    	public void status(String site)
    	{
    		try
    		{
    		Response con=Jsoup.connect(site).timeout(10000).userAgent("Mozilla/5.0 (compatible;)").execute();
    		System.out.println("Status Code:"+con.statusCode()+"  Message:"+con.statusMessage());
    		}
    		catch(Exception e)
    		{
    			System.out.println("Not able to fetch the page");
    		}
    	}
    }
    
    

    Here,

    1. We connect to the site making use of JSoup.We store the response in an response object.
    2. We make use of statusCode() function to obtain the status code from the response object and the statusMessage function to obtain the message on that status code.

    Full Source code:

    Listing 5:Full Source code

    package article15;
    
    import java.net.InetAddress;
    import org.jsoup.Jsoup;
    import org.jsoup.Connection.Response;
    
    public class WebPageStatus {
    
    	public static void main(String args[])
    	{
    		String siteToResolve="www.google.com";
    		String siteStatus="http://www.google.com";
    		new WebPageStatus().resolve(siteToResolve);
    		new WebPageStatus().status(siteStatus);
    	}
    	public void resolve(String site)
    	{
    		try
    		{
    		InetAddress addr = InetAddress.getByName(site);
    		System.out.println("Host Name:"+addr.getHostName());
    		System.out.println("Host Address:"+addr.getHostAddress());
    		}
    		catch(Exception e)
    		{
    			System.out.println("Unable to Resolve");
    		}
    	}
    	public void status(String site)
    	{
    		try
    		{
    		Response con=Jsoup.connect(site).timeout(10000).userAgent("Mozilla/5.0 (compatible;)").execute();
    		System.out.println("Status Code:"+con.statusCode()+"  Message:"+con.statusMessage());
    		}
    		catch(Exception e)
    		{
    			System.out.println("Not able to fetch the page");
    		}
    	}
    }
    
    

    Output:

    Host Name:www.google.com
    Host Address:74.125.236.147
    Status Code:200 Message:OK



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login