这是代码。
$url = 'https://******.aspx'
$req = [system.Net.WebRequest]::Create($url)
$expFlag = 0
$statusCode = $null
try {
$httpResponse = $req.GetResponse()
}
catch [System.Net.WebException] {
"Printing response code......"
Write-Host ([int]$httpResponse.StatusCode)
"***********"
$expFlag = 1
$expRes = $_.Exception.Response
}
if($expFlag -eq 0){
$statusCode = [int]$httpResponse.StatusCode
Write-Host $statusCode
Write-Host $expFlag
}
else{
$statusCode = ([int]$expRes.StatusCode)
Write-Host $statusCode
Write-Host $expFlag
}
if($statusCode -ge 403 -or $statusCode -eq 400){
Write-Host "Validation Failed"
}
else{
Write-Host "Validation Succeeded"
}
我已经掩盖了我公司特定的网址。此脚本在我的计算机上的 powershell ISE 中运行时运行良好。问题是当我在 Udeploy 中运行时,它适用于任何一般网站,如 google.com 等,但是当 url 是我的公司网站之一时,[int]$httpResponse.StatusCode 为 0 并且存在异常($ expFlag 为 1)。我了解 0 不是有效的 http 状态代码。如果我删除 [int] 强制转换,它通常应该给出文本代码,例如 200 的“OK”。但是 0 中没有任何内容。请帮助我理解为什么没有响应正文,为什么它会这样表现公司特定的应用程序,为什么它仅在通过 Udeploy 运行并且在 ISE 上运行正常时才会发生?