Wednesday, 21 May 2014

How to check Is web link is available or not

Check Is Web link is Available or not


Introduction

Here we find is given web link is valid or active link or fake link.

Description

Here we find is given web link is valid or active link or fake link.This we will done using Asp.net MVC

Solution

Step 1 :  Create your Asp.net MVC 4 application first using Visual Studio 2012 or Above .

Step 2 :  Now add below code to your project.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;


/// <summary>
/// Check is website available
/// </summary>
/// <param name="Url"></param>
/// <returns></returns>
 public static bool IsWebSiteAvailable(string Url)
        {
            string Message = string.Empty;
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Url);

            // Set the credentials to the current user account
            request.Credentials = System.Net.CredentialCache.DefaultCredentials;
            request.Method = "GET";

            try
            {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    // Do nothing; we're only testing to see if we can get the response
                }
            }
            catch (WebException ex)
            {
                Message += ((Message.Length > 0) ? "\n" : "") + ex.Message;
            }

            return (Message.Length == 0);
}

Step 3 :  Now access this function from your code and pass the url value of website link.

bool isWebLinkExists = ClassName.IsWebSiteAvailable("http://www.google.com");

bool isWebLinkExists = ClassName.IsWebSiteAvailable("http://www.tesingwebsite.com");



No comments:

Post a Comment

Thank you for your interest .