检查重定向的 URL 后如何在 PS 中获取 200 状态代码 [英] How to get 200 status code in PS after checking a redirected URL

查看:90
本文介绍了检查重定向的 URL 后如何在 PS 中获取 200 状态代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

如果我运行下面的 PS 脚本: .\CheckUrl.ps1 https://example.com/index?redirectUrl=

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

$url = $args[0]函数 Get-WebStatus($url) {尝试 {[Net.HttpWebRequest] $req = [Net.WebRequest]::Create($url)$req.Method = "HEAD"[Net.HttpWebResponse] $res = $req.GetResponse()如果 ($res.StatusCode -eq "200") {Write-Host "`n站点 $url is UP (返回代码:$($res.StatusCode) - $([int] $res.StatusCode))`n"} 别的 {Write-Host "`n站点 $url is DOWN (Return code: $($res.StatusCode) - $([int] $res.StatusCode))`n"}} 抓住 {Write-Host "`nThe site $url is DOWN (Return code: $($res.StatusCode) - $([int] $res.StatusCode))`n" -ForegroundColor Red -BackgroundColor Black}}获取 WebStatus $url

解决方案

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

#url1 = $args[0]函数 Get-WebStatus($url) {尝试 {$req = [System.Net.HttpWebRequest]::Create($url)$req.Method = "HEAD"$req.Timeout = 30000$req.KeepAlive = $false$res = $req.GetResponse()如果($res.StatusCode.value__ -eq 200){Write-Host ("`n站点 $url 已启动 (返回代码:" +"$($res.StatusCode) - " +"$($res.StatusCode.value__))`n") -ForegroundColor Cyan} 别的 {Write-Host ("`n站点 $url is DOWN (返回代码:" +"$($res.StatusCode) - " +"$($res.StatusCode.value__))`n") -ForegroundColor Yellow}} 抓住 {$res = $null ### 或 ### [System.Net.HttpWebRequest]::new()Write-Host ("`nThe site $url is DOWN " +"($($error[0].Exception.InnerException.Message))`n") - 前景红色}$res ### 返回一个值}#Get-WebStatus $url1

输出示例:

Get-WebStatus 'https://google.com/index?redirectUrl='获取 WebStatus 'https://google.com/'获取 WebStatus 'https://example.com/index?redirectUrl='

<块引用>

站点 https://google.com/index?redirectUrl= 已关闭(远程服务器返回错误:(404)未找到.)网站 https://google.com/已启动(返回代码:OK - 200)该站点 https://example.com/index?redirectUrl= 已关闭(操作已时间到)

I'm trying to get a 200 status code for a URL but I keep getting 0 instead. The url works, but the problem is it's a redirected URL. Even if I try the final URL after it redirects it still shows me a 0 status code.

How can I get the correct status code for a website that's down or up regardless of whether it's been redirected or not?

This is what I have now which works fine for regular URL's like http://google.com but not for redirected URL's. Unfortunately the urls I'm working with are private but it's in the format of http://example.com which winds up at https://example.com/index?redirectUrl=

If I run the PS script below with: .\CheckUrl.ps1 https://example.com/index?redirectUrl=

...it still fails to return a code of 200. The page comes up fine, whether I use the 1st url or the final redirected url but status code returns 0 which means it says the site is down and that's not true.

$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

解决方案

Too long for a comment. Important: $res = $req.GetResponse() does not set any value into the $res variable in the catch case (the $res variable keeps unchanged).

#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

Output examples:

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)

这篇关于检查重定向的 URL 后如何在 PS 中获取 200 状态代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆