在Powershell中获取HTTP请求并容忍500个服务器错误 [英] Get HTTP request and tolerate 500 server error in powershell
问题描述
在Powershell v3.0中,我想从HTTP GET返回响应代码,例如200 OK
或500 Internal Server Error
. (这是为了让黑客部署人员快速预热已部署的站点并查看其是否正常运行,这是一种小型的接受测试.状态代码确实是我想要的.)
In Powershell v3.0 I would like to return the response code from an HTTP GET, such as 200 OK
or 500 Internal Server Error
. (This is for a hack-deploy to do a quick warmup of a deployed site and see if it works, a sort of a mini acceptance test. The status code is truly all I want.)
违反我的意愿,HttpWebRequest.GetResponse
收到500 Internal Server Error
时抛出错误.这很烦人,因为在我的用例中,这并不是我真正的错误.无论如何,我认为我可以捕捉到异常并仍然剥离底层的响应代码,但是我对此感到麻烦.
Against my wishes, HttpWebRequest.GetResponse
throws an error when it receives a 500 Internal Server Error
. This is annoying because it isn't really an error to me in my use case. Anyway, I figured I could catch the exception and still peel out the underlying response code, but I'm having trouble with that.
这是一些几乎可以正常工作的代码:
Here's some almost-working code:
function WebResponseStatusCode (
[Parameter(Mandatory=$true)][string] $url
) {
$req = [system.Net.HttpWebRequest]::Create($url)
try {
$res = $req.GetResponse();
$statuscode = $res.statuscode;
}
catch [System.Net.WebException] {
#the outer error is a System.Management.Automation.ErrorRecord
Write-Host "error!"
return = $_.Response.statuscode; #nope
}
finally {
if (!($res -eq $null)) {
$res.Close();
}
}
return $statuscode;
}
问题当然是$_
没有Response
属性. $_.InnerException
也不会,即使在投射时也是如此:
The problem is of course that $_
has no Response
property. Neither does $_.InnerException
, even when cast:
return [System.Net.WebException]($_.InnerException)
我玩过$_ | Get-Member
并探索了其所有属性.我以为$_.TargetObject
有希望,但似乎没有.
I've played around with $_ | Get-Member
and exploring all its properties. I thought $_.TargetObject
had some promise but it doesn't appear to.
(更新)我也认为我尝试了$_.Exception.Response
的变体,尽管可能弄错了.
(Update) I also think I tried variations on $_.Exception.Response
though may have gotten it wrong.
仅获取响应代码似乎很简单.
Getting just a response code seems like such a simple thing to do.
推荐答案
这里是一个示例,尽管它做了很多其他事情,使您可以测试重定向以及预期的异常.
Here's an example, though it does a couple more things to allow you to test redirections as well as expected exceptions.
function GetWebSiteStatusCode {
param (
[string] $testUri,
$maximumRedirection = 5
)
$request = $null
try {
$request = Invoke-WebRequest -Uri $testUri -MaximumRedirection $maximumRedirection -ErrorAction SilentlyContinue
}
catch [System.Net.WebException] {
$request = $_.Exception.Response
}
catch {
Write-Error $_.Exception
return $null
}
$request.StatusCode
}
GetWebSiteStatusCode -testUri "https://www.google.com/"
GetWebSiteStatusCode -testUri "https://www.google.com/foobar"
GetWebSiteStatusCode -testUri "http://google.com/" -maximumRedirection 0
GetWebSiteStatusCode -testUri "https://accounts.google.com" -maximumRedirection 0
GetWebSiteStatusCode -testUri "https://www.googleapis.com/coordinate/v1/teams/1/custom_fields?fields=1111&key="
GetWebSiteStatusCode -testUri "https://www.googleapis.com/shopping/search/v1/test/products/sasdf/asdf/asdf?key="
#Next test would be for an expected 500 page.
#GetWebSiteStatusCode -testUri "https://www.somesite.com/someurlthatreturns500"
这篇关于在Powershell中获取HTTP请求并容忍500个服务器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!