![]() |
|
|
|||||||
![]() |
|
|
Thread Tools | Rate Thread |
|
|
PM User | #1 |
|
New to the CF scene Join Date: Nov 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
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");
}
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.. |
|
|
|
|
|
PM User | #2 |
|
Regular Coder ![]() Join Date: May 2005
Location: USA
Posts: 274
Thanks: 2
Thanked 5 Times in 5 Posts
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Tags |
| c# url |
| Thread Tools | |
| Rate This Thread | |
|
|