如何对500内部服务器错误做出反应? 让我们说我有以下代码:
$webpage = $webclient.DownloadString("http://moocow/Addfoo.asmx/roflmao?inputFoofoo=$variable")
在我的情况下,我有一个返回一些信息的页面;但是,有时页面会关闭并返回500内部服务器错误。如果是这种情况,如何告诉脚本执行另一个操作? 以下是我得到的错误:
Exception calling "DownloadString" with "1" argument(s): "The remote server returned an error: (500) Internal Server Error."
At \\ASERVER\AFOLDER\ASCRIPT.ps1:112 char:57
+ $global:webpage = $webclient.DownloadString <<<< ("http://moocow/Addfoo.asmx/roflmao?inputFoofoo=$variable")
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
答案 0 :(得分:1)
你可以像这样执行你的操作:
if($webpage)
{
perform operations
}
else
{
exceptional case
}
答案 1 :(得分:1)
您可以使用try / catch块:
$webPage = $null
try
{
$webPage = $webClient.DownloadString(...)
}
catch
{
"Error getting webpage!"
}
if($webPage)
{
"Getting the webpage worked!"
}