0

I've got a function that's SUPPOSED to check if an url exists.

Function URLExists(URL As String) As Boolean
Dim HttpWebRequest_ As HttpWebRequest = WebRequest.Create(URL)
HttpWebRequest_.Method="HEAD" 'It doesn't work even without this.
Dim HttpWebResponse_ As HttpWebResponse = HttpWebRequest_.GetResponse()
Return HttpWebResponse_.StatusCode = HttpStatusCode.OK
End Function

If I try URLExists("http://www.google.com/thisPageDoesNotExistAndIsRetarded.html"), it returns True, and if I try URLExists("https://www.google.com/"), (which obviously exists), it returns True, (correct). Thanks.

1 Answers1

0

I'm not familiar with VB, but I've write some c#:

string url = "http://www.google.com/thisPageDoesNotExistAndIsRetarded.html";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "HEAD";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

And a [System.Net.WebException] (Error: 404) is thrown at runtime at the GetResponse(). It is working.

Perhaps you can try to add a try{ } catch{ return false} in your method.

Justin Lessard
  • 10,804
  • 5
  • 49
  • 61