首页 > 解决方案 > 区分 invoke-WebRequest 状态码

问题描述

我正在尝试在下载这些文件之前验证 URL,并且我想区分需要凭据的路径与错误路径之类的东西。

我现在有这个

function Get-UrlStatusCode([string] $Url) {
    try {
        return (Invoke-WebRequest -Uri $Url -UseBasicParsing -DisableKeepAlive -Method:head).StatusCode
    } catch [Net.WebException] {
        return "$([Int]$_.Exception.Response.StatusCode) $($_.Exception.Response.statusDescription)"
    }
}

(Get-UrlStatusCode 'http://AWSBUCKETPATH/Test_Public.xml')
(Get-UrlStatusCode 'http://AWSBUCKETPATH/Test_Private.xml')
(Get-UrlStatusCode 'http://AWSBUCKETPATH/Test_Missing.xml')

对于公共路径,我得到预期的 StatusCode 200,但私有示例和缺少的示例都返回 403 Forbidden。这是我的代码中的故障,还是需要在 AWS 上进行配置才能为错误路径提供 404 Not Found,或者尽管存在 404 作为代码,但这不是可能的事情?

标签: powershellhttp-status-codesinvoke-webrequest

解决方案


推荐阅读