VB.NET how to check if a webpage exists

[亡魂溺海] 提交于 2019-12-11 03:02:32

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!