问题
Is it possible to check if a webpage exists or not in vb.net application ?
回答1:
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();
回答2:
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
回答3:
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.
回答4:
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
来源:https://stackoverflow.com/questions/14385798/vb-net-how-to-check-if-a-webpage-exists