Trying to detect whether an url link is valid or not

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • colol
    New Member
    • Aug 2012
    • 8

    #1

    Trying to detect whether an url link is valid or not

    Good morning, does anyone know how to detect whether a url link is valid or not. I was ask to create a function that detects whether a url link is valid or not, and if it is not valid then certain actions take place, but for now i am not sure how to detect the url, i have tried many coding but it seems to not work at all. please help me!!
  • PsychoCoder
    Recognized Expert Contributor
    • Jul 2010
    • 465

    #2
    By 'valid' do you mean it's formatted correctly it a URL that actually exists? You say you've tried some coding but cannot get it to work, show us what you've tried and it'll be easier for us to help.

    Comment

    • colol
      New Member
      • Aug 2012
      • 8

      #3
      This is the coding i did, but whether it's only http or with www, it only displays Invalid URL. eg. https://www.google.com.my/ & http://forums.asp.net/

      Code:
      protected void Page_Load(object sender, EventArgs e)
          {
      
              Uri url = new Uri("http//...");
              WebRequest request = WebRequest.Create(url);
              request.Method = "HEAD";
              WebResponse r;
      
              try
              {
                  r = request.GetResponse();
                  r.Close();
                  Response.Write("Valid URL");
              }
              catch (Exception)
              {
                  Response.Write("Invalid URL");
              }
      
          }
      Last edited by PsychoCoder; Sep 3 '12, 04:16 AM. Reason: Code tags added

      Comment

      • PsychoCoder
        Recognized Expert Contributor
        • Jul 2010
        • 465

        #4
        Try changing your code to this (it will then show you what's going wrong:

        Code:
        protected void Page_Load(object sender, EventArgs e)
            {
         
                Uri url = new Uri("http//...");
                WebRequest request = WebRequest.Create(url);
                request.Method = "HEAD";
                WebResponse r;
         
                try
                {
                    r = request.GetResponse();
                    r.Close();
                    Response.Write("Valid URL");
                }
                catch (Exception ex)
                {
                    //Show the exact error
                    Response.Write(ex.ToString());
                }
         
            }

        Comment

        • colol
          New Member
          • Aug 2012
          • 8

          #5
          another question i have is, is it possible to add gridview inside the try-catch statement, like this:

          Code:
          try 
                  { 
                      r = request.GetResponse(); 
                      r.Close(); 
                      GridView1.DataSource = RSS(url);
                      GridView1.DataBind();
          }
          catch (Exception) 
                  { 
                      Response.Write("Invalid URL"); 
                  }
          Last edited by PsychoCoder; Sep 3 '12, 05:04 AM. Reason: Code tags added

          Comment

          Working...