0

我有一个应该检查url是否存在的函数。

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,(正确)。谢谢。

4

1 回答 1

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(). 这是工作。

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

于 2013-10-24T02:50:25.417 回答