VB.NET如何检查网页是否存在

时间:2013-01-17 18:34:35

标签: vb.net webpage

是否可以检查vb.net应用程序中是否存在网页?

4 个答案:

答案 0 :(得分:0)

您可以执行此操作以获取网页文本。

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();

答案 1 :(得分:0)

您可以通过请求相关网页并查看是否有错误消息来查找。

    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

答案 2 :(得分:0)

您不需要“If”语句来测试不存在。只需在“catch”语句之后立即执行代码即可处理该可能性。 只有在搜索中出现错误(“WebRequest”)时才会运行该代码。错误是找不到页面时。

答案 3 :(得分:0)

 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