【问题标题】:How to get 200 status code in PS after checking a redirected URL检查重定向 URL 后如何在 PS 中获取 200 状态码
【发布时间】:2019-02-14 14:57:09
【问题描述】:

我正在尝试为 URL 获取 200 状态代码,但我一直得到 0。该网址有效,但问题是它是一个重定向的网址。即使我在重定向后尝试最终 URL,它仍然会显示 0 状态代码。

无论是否被重定向,我如何才能为关闭或启动的网站获取正确的状态代码?

这就是我现在所拥有的,它适用于像 http://google.com 这样的常规 URL,但不适用于重定向的 URL。不幸的是,我正在使用的网址是私有的,但格式为http://example.com,最后是https://example.com/index?redirectUrl=

如果我使用以下命令运行 PS 脚本:.\CheckUrl.ps1 https://example.com/index?redirectUrl=

...它仍然无法返回 200 的代码。无论我使用第一个 url 还是最终重定向的 url,页面都正常显示,但状态代码返回 0,这意味着它表示站点已关闭,这不是真的.

$url = $args[0]
function Get-WebStatus($url) {
    try {
        [Net.HttpWebRequest] $req = [Net.WebRequest]::Create($url)
        $req.Method = "HEAD"
        [Net.HttpWebResponse] $res = $req.GetResponse()
        if ($res.StatusCode -eq "200") {
            Write-Host "`nThe site $url is UP (Return code: $($res.StatusCode) - $([int] $res.StatusCode))`n"
        } else {
            Write-Host "`nThe site $url is DOWN (Return code: $($res.StatusCode) - $([int] $res.StatusCode))`n"
        }
    } catch {
        Write-Host "`nThe site $url is DOWN (Return code: $($res.StatusCode) - $([int] $res.StatusCode))`n" -ForegroundColor Red -BackgroundColor Black
    }
}
Get-WebStatus $url

【问题讨论】:

  • (404) Not Found 来自Invoke-WebRequest -Uri 'https://example.com/index?redirectUrl=' 的错误。你永远不会得到 (200) OKexample.com
  • 网址 example.com 只是一个假网址。我正在测试的 url 是私有的,所以我不能透露它们,但关键是它们都出现在 IE 中。但我认为,因为他们重定向上面的脚本报告状态代码 0。如果网站已启动并运行,那么它应该报告 200...即使网站被重定向。
  • 我认为下面的代码会有所帮助: $url = google.com [Net.ServicePointManager]::SecurityProtocol = "Tls12, Tls11, Tls" $httpStatus = $url.StatusCode invoke-webrequest -uri $url -DisableKeepAlive -UseBasicParsing -Method head 这将满足我的需要,但我需要有人帮助不打印此输出并添加 if else 语句,以便 if status code = 200 write-host "Up" else write-host “下”

标签: powershell url


【解决方案1】:

评论太长了。重要提示:$res = $req.GetResponse() 不会在 catch 情况下为 $res 变量设置任何值($res 变量保持不变)。

#url1 = $args[0]
function Get-WebStatus($url) {
    try {
        $req = [System.Net.HttpWebRequest]::Create($url)
        $req.Method    = "HEAD"
        $req.Timeout   = 30000
        $req.KeepAlive = $false
        $res = $req.GetResponse()
        if ($res.StatusCode.value__ -eq 200) {
            Write-Host ("`nThe site $url is UP (Return code: " + 
                "$($res.StatusCode) - " + 
                "$($res.StatusCode.value__))`n") -ForegroundColor Cyan
        } else {
            Write-Host ("`nThe site $url is DOWN (Return code: " +
                "$($res.StatusCode) - " + 
                "$($res.StatusCode.value__))`n") -ForegroundColor Yellow
        }
    } catch {
        $res = $null  ### or ### [System.Net.HttpWebRequest]::new()
        Write-Host ("`nThe site $url is DOWN " + 
            "($($error[0].Exception.InnerException.Message))`n") -Foreground Red
    }
    $res    ### return a value
}
#Get-WebStatus $url1

输出示例:

Get-WebStatus 'https://google.com/index?redirectUrl='
Get-WebStatus 'https://google.com/'
Get-WebStatus 'https://example.com/index?redirectUrl='
The site https://google.com/index?redirectUrl= is DOWN (The remote server
 returned an error: (404) Not Found.)

The site https://google.com/ is UP (Return code: OK - 200)

The site https://example.com/index?redirectUrl= is DOWN (The operation has
 timed out)

【讨论】:

  • @yorkman 如果我的回答有帮助,请考虑将其标记为已接受。 See this page 解释为什么这很重要。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-11
  • 2018-09-05
  • 1970-01-01
相关资源
最近更新 更多