You can use the following function in a Web Service or any other types of
project.
Juz pass a URL to the function and it will return true if the URL is valid.
' This function checks whether a file exists on a remote server.
' It will return True if that file exists, and False if it doesn't.
'
' Depending on the Web Server which is hosting your file,
' the url to your file may/maynot be case-sensitive
' e.g. the following url will return True
' http://sg.geocities.com/mangokun/images/settings.ico
' HttP://sG.geOcities.com/mAngokun/images/settings.ico
' but the rest below will return False
' http://sg.geocities.com/mangokun/imageS/settings.ico
' http://sg.geocities.com/mangokun/images/Settings.ico
' therefore in my case, sub-dirs and files must follow what I named
them in my web dir.
<WebMethod()> Public Function IfWReqFileExist(ByVal strUri As String)
As Boolean
Try
Dim WReq As WebRequest = WebRequest.Create(strUri)
Dim WResp As WebResponse = WReq.GetResponse()
WResp.Close()
Return True
Catch
Return False
End Try
End Function
' If you like, you can use this instead of the one above
<WebMethod()> Public Function IfHttpWReqFileExist(ByVal strHttpUri As
String) As Boolean
Try
Dim HttpWReq As HttpWebRequest = CType(WebRequest.Create
(strHttpUri), HttpWebRequest)
' Turn off connection keep-alives.
HttpWReq.KeepAlive = False
Dim HttpWResp As HttpWebResponse = CType(HttpWReq.GetResponse
(), HttpWebResponse)
HttpWResp.Close()
Return True
Catch
Return False
End Try
End Function
Previous message:
> I used to have a link-checker script that would go through a table of
URLs in a database and issue a request/response to check their server
status code and description, and report this info back to me. It wasn't
very scalable, and I am trying to re-write in ASP.NET.After some testing,
I am confused as to how to do this with ASP.NET. It would seem that the
HttpWebRequest and HttpWebResponse classes in the System.Net namespace
should do the trick, as the latter has StatusCode and StatusDescription
properties.But when I try the code below with test URLs, I don't get the
desired results. If I try a known URL, like my own
http://www.epistemelinks.com/, then I get back a statuscode of OK as I
should. If I try one that should return 404 error, I still get OK. If I
try one that doesn't even resolve as a domain name, then one of the
exceptions fires instead (which is fine).I am using the wrong classes for
what I am trying to do? How can I write some simple code to properly check
for 200 = OK vs. 404 = Page Not Found vs. 500 = Error and so forth?
Thanks,Tom S. private void Page_Load(object sender, System.EventArgs e){
try { HttpWebRequest objHttpWebRequest = (HttpWebRequest)
WebRequest.Create("http://www.epistemelinks.com/nopagehere.aspx");
HttpWebResponse objHttpWebResponse = (HttpWebResponse)
objHttpWebRequest.GetResponse(); string strData =
objHttpWebResponse.StatusCode.ToString() + " "; strData +=
objHttpWebResponse.StatusDescription + " "; strData +=
objHttpWebResponse.LastModified.ToString(); objHttpWebResponse.Close
(); Heading.InnerHtml = "ELC Link Checker Tool"; Intro.InnerHtml
= "Your query returned the following report:"; SearchResults.InnerHtml
= strData;} catch(WebException ex) { SearchResults.InnerHtml
= "WebException raised: " + ex.Status; } catch(Exception ex) {
SearchResults.InnerHtml = "xception raised: " + ex.Message; }}