System.Net.HttpWebRequest question

J

James

I don't know if I'm barking up the wrong tree or not, but here goes.
Basically I just want to validate that a URL exists when a user enters it
into our system. Essentially a send out a little request to see if the page
actually loads or if it doesn't exist at all.

I've been using the following code I lifted, but I'm not sure where to take
it from here, or if I have the entirely wrong namespace/concept.

Dim request As HttpWebRequest =
CType(WebRequest.Create("http://www.thissitewontexist.com"), HttpWebRequest)

' Set some reasonable limits on resources used by this request
request.MaximumAutomaticRedirections = 4
request.MaximumResponseHeadersLength = 4

' Set credentials to use for this request.
request.Credentials = CredentialCache.DefaultCredentials

Dim responser As HttpWebResponse
Try
responser = CType(request.GetResponse(), HttpWebResponse)
Catch
' Gets here if it fails, but I'd prefer a cleaner solution
End Try

After this block, StatusCode = OK for sites that worked.

I'm completely lost on how to do this smoothly, any thoughts?

Thanks,
James
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,055
Members
48,769
Latest member
Clifft

Latest Threads

Top