locked
Simply check whether url link active or not RRS feed

  • Question

  • User1253203540 posted

    Anyone know how to do a simple function where if the url link given exists simply output website is found and if not website is not found??

    Monday, August 6, 2012 11:31 PM

All replies

  • User-287491473 posted

    Something like this should work:

            protected void Page_Load(object sender, EventArgs e)
            {
                Response.Write(IsLinkValid(new Uri("http://www.asp.net")));
                Response.Write("<br />");
                Response.Write(IsLinkValid(new Uri("http://www.asp.net/ThisDoesNotExist")));
                Response.Write("<br />");
            }
    
            private Boolean IsLinkValid(Uri uri)
            {
                try
                {
                    WebRequest request = WebRequest.Create(uri);
                    request.Method = "HEAD";
                    WebResponse r = request.GetResponse();
                    return true;
                }
                catch (WebException)
                {
                    return false;
                }
            }

    Tuesday, August 7, 2012 12:16 PM
  • User1253203540 posted

    Thankyou for the coding but when i run the coding, both output False....

    Sunday, September 2, 2012 10:50 PM