Reputation: 235
Is it possible to check if a webpage exists or not in vb.net application ?
Upvotes: 2
Views: 14291
Reputation: 21
Private Function RemoteFileExists(ByVal url As String) As Boolean
Try
Dim request As HttpWebRequest = TryCast(WebRequest.Create(url), HttpWebRequest)
request.Method = "HEAD"
Dim response As HttpWebResponse = TryCast(request.GetResponse(), HttpWebResponse)
response.Close()
Return (response.StatusCode = HttpStatusCode.OK)
Catch
Return False
End Try
End Function
Upvotes: 1
Reputation: 61
You do not need an "If" statement to test for the non-existence. Simply plce the code to handle that eventuality immediately after the "catch" statement. That code will only run if an error occurrs in the search ("WebRequest"). The error is when the page is not found.
Upvotes: 0
Reputation: 12748
You can find out by requesting the webpage in question and looking if there's an error message.
Dim req As System.Net.WebRequest
Dim res As System.Net.WebResponse
req = System.Net.WebRequest.Create("http://www.google.com/werwerfsdfsdf")
Try
res = req.GetResponse()
Catch e As WebException
' URL doesn't exists
End Try
Upvotes: 1
Reputation: 15978
You can do this to get the text of a web page.
string strUrl = "http://forum.codecall.net/external.php?type=RSS2";
WebRequest request = WebRequest.Create(strUrl);
WebResponse response = request.GetResponse();
string data = new System.IO.StreamReader(response.GetResponseStream()).ReadToEnd();
Upvotes: 0