VB.NET使用WebRequest检查URI是否有效 [英] VB.NET Use WebRequest to check if URI is valid
问题描述
寻找一种无需下载实际内容即可确定VB.NET中是否存在URI的最佳方法.可以在本地使用System.IO.FileExists
/My.Computer.FileSystem.FileExists
来确定文件是否存在,Web是否有等效文件?
Looking for the best way to determine if a URI exists in VB.NET without downloading the actual content. System.IO.FileExists
/My.Computer.FileSystem.FileExists
can be used locally to determine if a file exists, is there an equivalent for the Web?
当前,我正在使用HttpWebRequest
来使用ResponseStream
检查URI是否存在.如果目标确实存在,则将填充流,如果目标不存在,则将引发异常.该功能已被扩展为还可以检查PDF文件(通常为5MB以上),图像等,将内容实际填充到流中会浪费时间/带宽.
Currently I am using a HttpWebRequest
to check URI existance using the ResponseStream
. This populates the stream if the target does exist and throws an exception if it doesn't. The function is being expanded to also check for PDF files (typically 5MB +), images, etc and it will be a waste of time/bandwidth to actually populate the content into a stream.
对于成功"(目标确实存在)的情况,我不希望下载文件或页面,只是希望以布尔值结尾,该布尔值指示在此URI的末尾是否存在某些内容.
In the case of "Success" (the target does exist) I do not wish to download the file or page, simply to end up with a Boolean which indicates the whether something exists at the end of this URI.
推荐答案
此处在VB.NET中.确保您的地址以 http://或 https://开头.
Here it is in VB.NET. Make sure that your addresses start with http:// or https://.
Public Function CheckAddress(ByVal URL As String) As Boolean
Try
Dim request As WebRequest = WebRequest.Create(URL)
Dim response As WebResponse = request.GetResponse()
Catch ex As Exception
Return False
End Try
Return True
End Function
这篇关于VB.NET使用WebRequest检查URI是否有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!