无效 URL 与未找到
Invalid URL vs not found
我希望验证 URLs 与使用 Test-Path 对文件系统和注册表路径所做的类似。
但是,当然,Test-Path
不能在 URL 上运行,而且我一直无法在 PowerShell 中找到执行此操作的方法。
我可以使用 Invoke-WebRequest
,但据我所知没有验证,如果找到,我可以获得 return 代码 200,如果没有找到,则为 404。
唯一的例外是无效的主机名,例如 host,com
,这让我想知道:
除了主机名无效,还有什么无效的URL吗?
或者一旦正确定义了端口和主机,它基本上是 URL 路径中有效的任何字符吗?
我认为你可以用不同的方式来解决这个问题。
- 使用正则表达式
检查URL格式是否正确
- 解析那个url后面的IP地址,这会让你知道那个地址后面是否有东西。
检查下面的示例:
#1 URL format validation
#input the URL here
$urlInput = 'www.google.com'
#This is the regex pattern you can use to validate the format - reference : https://www.regextester.com/94502
$regEx="^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!$&'\(\)\*\+,;=.]+$"
if($urlInput -match $regEx){
Write-Host "$urlInput is a Valid URL!"
#2 is there a server behind this url
try{
Resolve-DnsName -Name $urlInput -ErrorAction Stop
}catch{
if($_ -like '*DNS name does not exist*'){
Write-Host "No DNS record for the following URL : $urlInput"
}else{
Write-Output $_
}
}
}
else{
Write-Host "Invalide URL - $urlInput "
}
P.S。我使用了以下表达式 - https://www.regextester.com/94502 ,您可以使用它来匹配您的用例。
您可以使用System.Uri.IsWellFormedUriString
方法来测试URI (URL)是否好- formed,即formally valid(不管域是否存在,可达,路径是否存在,...)。
要额外确保给定的 URI 仅限于特定 URI 方案 ,例如 http://
和 https://
,您可以执行以下操作:
$uri = 'https://example.org/foo?a=b&c=d%20e'
[uri]::IsWellFormedUriString($uri, 'Absolute') -and ([uri] $uri).Scheme -in 'http', 'https'
请注意,给定的 URI 必须已经包含 escaped 形式的保留字符 才能被视为格式良好;例如,空格必须编码为 %20
,如上例所示,System.Uri.EscapeDataString
方法可以对 URI 的构成(非语法)部分执行(例如 [uri]::EscapeDataString('a b')
)
我希望验证 URLs 与使用 Test-Path 对文件系统和注册表路径所做的类似。
但是,当然,Test-Path
不能在 URL 上运行,而且我一直无法在 PowerShell 中找到执行此操作的方法。
我可以使用 Invoke-WebRequest
,但据我所知没有验证,如果找到,我可以获得 return 代码 200,如果没有找到,则为 404。
唯一的例外是无效的主机名,例如 host,com
,这让我想知道:
除了主机名无效,还有什么无效的URL吗?
或者一旦正确定义了端口和主机,它基本上是 URL 路径中有效的任何字符吗?
我认为你可以用不同的方式来解决这个问题。
- 使用正则表达式 检查URL格式是否正确
- 解析那个url后面的IP地址,这会让你知道那个地址后面是否有东西。
检查下面的示例:
#1 URL format validation
#input the URL here
$urlInput = 'www.google.com'
#This is the regex pattern you can use to validate the format - reference : https://www.regextester.com/94502
$regEx="^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!$&'\(\)\*\+,;=.]+$"
if($urlInput -match $regEx){
Write-Host "$urlInput is a Valid URL!"
#2 is there a server behind this url
try{
Resolve-DnsName -Name $urlInput -ErrorAction Stop
}catch{
if($_ -like '*DNS name does not exist*'){
Write-Host "No DNS record for the following URL : $urlInput"
}else{
Write-Output $_
}
}
}
else{
Write-Host "Invalide URL - $urlInput "
}
P.S。我使用了以下表达式 - https://www.regextester.com/94502 ,您可以使用它来匹配您的用例。
您可以使用System.Uri.IsWellFormedUriString
方法来测试URI (URL)是否好- formed,即formally valid(不管域是否存在,可达,路径是否存在,...)。
要额外确保给定的 URI 仅限于特定 URI 方案 ,例如 http://
和 https://
,您可以执行以下操作:
$uri = 'https://example.org/foo?a=b&c=d%20e'
[uri]::IsWellFormedUriString($uri, 'Absolute') -and ([uri] $uri).Scheme -in 'http', 'https'
请注意,给定的 URI 必须已经包含 escaped 形式的保留字符 才能被视为格式良好;例如,空格必须编码为 %20
,如上例所示,System.Uri.EscapeDataString
方法可以对 URI 的构成(非语法)部分执行(例如 [uri]::EscapeDataString('a b')
)