我有一个巨大的博客网址列表,我需要检查其有效性。我从this answer和here那里敲了一个剧本。
这是我的脚本:
$siteURL = 'http://example.com/'
$File = '.\urls.txt'
$NewContent = Get-Content -Path $File | ForEach-Object {
$_
$HTTP_Request = [System.Net.WebRequest]::Create($siteURL + $_)
$HTTP_Response = $HTTP_Request.GetResponse()
$HTTP_Status = [int]$HTTP_Response.StatusCode
if ($HTTP_Status -eq 200) {
" - 200"
} else {
" - " + $HTTP_Status
}
$HTTP_Response.Close()
}
$NewContent | Out-File -FilePath $File -Encoding Default -Force
我的问题是,当它出现404错误时,它不会将其添加到文件中并在控制台中返回以下错误:
Exception calling "GetResponse" with "0" argument(s): "The remote server
returned an error: (404) Not Found."
At C:\Users\user.name\urlcheck.ps1:19 char:9
+ $HTTP_Response = $HTTP_Request.GetResponse()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
为什么我收到此错误?
奖金问题:我的“200 - OK”回复正在添加到新的一行,为什么?
为了处理404
响应(以及类似的错误响应),我们需要一些错误处理代码:
ForEach-Object {
$_
$HTTP_Request = [System.Net.WebRequest]::Create($siteURL + $_)
try {
$HTTP_Response = $HTTP_Request.GetResponse()
}
catch [System.Net.WebException] {
# HTTP error, grab response from exception
$HTTP_Response = $_.Exception.Response
}
catch {
# Something else went horribly wrong, maybe abort?
}
$HTTP_Status = [int]$HTTP_Response.StatusCode
If ($HTTP_Status -eq 200) {
" - 200"
}
Else {
" - " + $HTTP_Status
}
$HTTP_Response.Close()
}
奖金问题:我的200 -OK响应会被添加到新行中,为什么?
那是因为你在两个单独的陈述中输出$_
和" - " + ...
。从顶部删除$_
并将其全部合并为一个字符串:
ForEach-Object {
$HTTP_Request = [System.Net.WebRequest]::Create($siteURL + $_)
try {
$HTTP_Response = $HTTP_Request.GetResponse()
}
catch [System.Net.WebException] {
# HTTP error, grab response from exception
$HTTP_Response = $_.Exception.Response
}
catch {
# Something else went horribly wrong, maybe abort?
}
finally {
# Grab status code and dispose of response stream
$HTTP_Status = [int]$HTTP_Response.StatusCode
$HTTP_Response.Dispose()
}
"$_ - $HTTP_Status"
}
.NET实现在这一点上设计得有点糟糕。 WebRequest
不幸抛出错误状态代码。
基于this答案,您可以使用以下解决方法:
$siteURL = 'http://example.com/'
$file = '.\urls.txt'
(Get-Content $file) | foreach {
$HTTP_Response = $null
try {
$HTTP_Request = [System.Net.WebRequest]::Create($siteURL + $_)
$HTTP_Response = $HTTP_Request.GetResponse()
}
catch [System.Net.WebException] {
# catch this specific exception and get the response from it
$HTTP_Response = $_.Exception.Response
}
catch {
# for other errors, output the error message:
"{0} - ERROR: {1}" -f $_, $_.Exception.Message
continue
}
finally {
# standard handling of IDisposable
if ($HTTP_Response) { $HTTP_Response.Dispose() }
}
$HTTP_Status = $HTTP_Response.StatusCode
# NOTE: This will also fix your "newline" problem
"{0} - {1} ({2})" -f $_, [int]$HTTP_Status, $HTTP_Status
} | Out-File $file -Force