检查URL是否存在不起作用

时间:2013-10-24 02:10:45

标签: .net vb.net httpwebrequest httpwebresponse

我有一个 SUPPOSED 的功能来检查网址是否存在。

Function URLExists(URL As String) As Boolean
Dim HttpWebRequest_ As HttpWebRequest = WebRequest.Create(URL)
HttpWebRequest_.Method="HEAD" 'It doesn't work even without this.
Dim HttpWebResponse_ As HttpWebResponse = HttpWebRequest_.GetResponse()
Return HttpWebResponse_.StatusCode = HttpStatusCode.OK
End Function

如果我尝试URLExists(“http://www.google.com/thisPageDoesNotExistAndIsRetarded.html”),则返回True,如果我尝试URLExists(“https://www.google.com/”)(显然存在),则返回True,(正确)。感谢。

1 个答案:

答案 0 :(得分:0)

我不熟悉VB,但我写了一些c#:

string url = "http://www.google.com/thisPageDoesNotExistAndIsRetarded.html";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "HEAD";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

[System.Net.WebException]运行时抛出GetResponse()(错误:404)。 这是工作。

也许您可以尝试在方法中添加try{ } catch{ return false}