0

I am working on a form in which users are asked to provide a file's URL. I need to check if that URL really points to something. I use a CustomValidator with server-side validation. Here is the code :

Protected Sub documentUrlValide_ServerValidate
(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
Handles documentUrlValide.ServerValidate

    Try
        Dim uri As New Uri(args.Value)
        Dim request As HttpWebRequest = HttpWebRequest.Create(uri)
        Dim response As HttpWebResponse = request.GetResponse()
        Dim stream As Stream = response.GetResponseStream()
        Dim reader As String = New StreamReader(stream).ReadToEnd()
        args.IsValid = True
    Catch ex As Exception
        args.IsValid = False
    End Try

End Sub

I tested it with several valid URLs, none passed the test, the request.GetResponse() always throws a WebException : "can not resolve distant name".

What is wrong with this code ?

Update :

I couldn't make this work server-side, despite my code apparently being fine, so I ran it client-side with a javascript synchronous HTTP request. Here is the code (note that my application is only requested to run on IE, this code won't work on other browsers dut to Http request calls being different)

function testURLValide(sender, args)
{
    var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    xmlhttp.open("HEAD", args.Value, false);
    xmlhttp.send(null);

    if (! (xmlhttp.status == 400 || xmlhttp.status == 404))
    {
        args.IsValid = true;
    }
    else
    {
        args.IsValid = false;
    }
}
Thibault Witzig
  • 2,060
  • 2
  • 19
  • 30

4 Answers4

0

If you can use javascript, here's one really good way to do it: How to Test a URL in jQuery

Edit: How about one of these approaches then?

Search for "Does a URL exist?" on this page.

How to check if a URL exists in javascript

Javascript to check if URL exist

Community
  • 1
  • 1
DOK
  • 32,337
  • 7
  • 60
  • 92
0

Why not use WebClient.DownloadString() method instead?

decyclone
  • 30,394
  • 6
  • 63
  • 80
0

I put your code into LINQPad and tested it out and it worked just fine... The only difference is args.Value.

Dim uri As New Uri("http://www.google.com")
Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(uri)
Dim response As System.Net.HttpWebResponse = request.GetResponse()
Dim stream As Stream = response.GetResponseStream()
Dim reader As String = New StreamReader(stream).ReadToEnd()

reader.Dump()

<!doctype html><html><head><meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"><title>Google</title>............

phillip
  • 2,618
  • 19
  • 22
  • I put this exact code in my app and it doesn't work. I wonder if this could be related to a firewall rule in my office. I'll take a look at this. – Thibault Witzig Dec 15 '10 at 08:24
0

Instead of reading the stream, how about testing for the HTTP Status Code? I believe it is response.StatusCode. The value you would look for would be 200, or possibly something in the 300s (redirect).

Sam Axe
  • 33,313
  • 9
  • 55
  • 89
  • That is soemthing I plan to do to improve my validation mechanism. Unfortunately, I'm not that far at the moment, my code throws an exception before returning a status code. – Thibault Witzig Dec 15 '10 at 08:38