Go Back   CodingForums.com > :: Server side development > ASP.NET

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 11-07-2009, 09:03 PM   PM User | #1
ninjatruck
New to the CF scene

 
Join Date: Nov 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
ninjatruck is an unknown quantity at this point
C# Checking for valid URL

I am trying to write a web app that will check to see if a given URL is valid. This is the code i have so far:


Code:
WebRequest wr = WebRequest.Create(url);
        wr.Method = WebRequestMethods.Http.Head;
        try
        {
            using (HttpWebResponse response =   (HttpWebResponse)wr.GetResponse())
            {
                Label1.Text = (response.StatusCode.ToString());
            }
        }

        catch (Exception)
        {
            Label1.Text = ("Invalid URL");

        }
It works for checking if http://www.google.com, Label1 reads "OK" after, and it also works with a non existent page, such as http://www.google.com/notapage, but I'm trying to check to see if, for example, "http://www.amazon.com/exec/obidos/ASIN/B000GELXHY" is a valid URL, and after the code is executed, Label1 reads "Invalid URL", even though this is a valid URL.

I am new to C#, so I may be using the wrong class for what I'm trying to do. Any help would be greatly appreciated.

P.S. I don't know if it's relevant, but the code is being executed upon a button click.
P.S.S. replace "url" above with whatever url i am currently testing

Last edited by ninjatruck; 11-07-2009 at 09:06 PM..
ninjatruck is offline   Reply With Quote
Old 11-08-2009, 03:52 AM   PM User | #2
Freon22
Regular Coder

 
Freon22's Avatar
 
Join Date: May 2005
Location: USA
Posts: 274
Thanks: 2
Thanked 5 Times in 5 Posts
Freon22 is an unknown quantity at this point
Ok I redid your code a little and it shows good url where it is good and bad url where they return an error 404. I made it a little simpler then yours hope this help. btw could you please post back if this help or even if it doesn't. Thanks
Code:
using System.Net;


protected void btnTestURL_Click(object sender, EventArgs e)
    {
        try
        {
            string url = TextBox1.Text;
            if (url != "")
            {
                WebRequest Irequest = WebRequest.Create(url);
                WebResponse Iresponse = Irequest.GetResponse();
                if (Iresponse != null)
                {
                    Label1.Text = "This is a good URL.";
                }
            }
        }
        catch (Exception ex)
        {
            Label1.Text = "This is a bad URL it returns an ERROR 404 >>>>>>>> " + ex.ToString();
        }
    }
__________________
Power tends to corrupt, and absolute power corrupts absolutely.
Lord Acton 1887
Freon22 is offline   Reply With Quote
Reply

Bookmarks

Tags
c# url

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:57 AM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.