c# - Is there a surefire way to validate the existence of a URL? -
i have c# mvc4 website, call foo.org. in there "pages" such foo.org/news or foo.org/events. http website, not https.
i have c# mvc4 website, http site, administers first 1 (on same webserver). in latter website need validate existence of pages such "foo.org/news" in first one.
both intranet sites.
foo.org/news , foo.org/events valid pages. if slap urls on address bar of browser, appear,... no problem.
i tried several suggestion , other forums no avail.
things tried:
httpwebrequest request = webrequest.create(uri) httpwebrequest; request.method = "head"; httpwebresponse response = request.getresponse() httpwebresponse); var retval = (response.statuscode == httpstatuscode.ok); response.close(); return retval;
and
var pingsender = new ping(); var options = new pingoptions(); options.dontfragment = true; var data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; var buffer = encoding.ascii.getbytes(data); var timeout = 120; var reply = pingsender.send(uri, timeout, buffer, options); return (reply.status == ipstatus.success);
and
iphostentry iphost = dns.gethostentry(safehost); return (iphost.hostname != null && iphost.hostname != "");
in each, tried , without prepending "http://".
now combine 3 trying 1 after other , return first success encounter. can validate urls such google.com or foo.org not when have "/news" appended foo.org.
the above code not have it, meat of it.
sorting options how validate url worst best:
- your 3rd option looks dns record url. return true if there isn't web server listening ip.
- the 2nd option ping web server, aren't guaranteed pings allowed (which give false negative) or device responding actual webserver (which give false positive).
- the 1st option talk webserver verify page, not servers respond correctly
head
request (which problem in case). still have false positives , false negatives, better above options. - a variant of 1st option change http method
get
request instead. ask webserver actual webpage. give correct answer see if webpage exists, there cases not correct answer.
Comments
Post a Comment