"远程服务器返回错误:(401)未经授权 [英] " The remote server returned an error: (401) Unauthorized "

查看:493
本文介绍了"远程服务器返回错误:(401)未经授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试验证我的URL是否得到响应. 换句话说,我正在尝试检查身份验证是否已成功访问该网站.

我用过:

 $HTTP_Request = [System.Net.WebRequest]::Create('http://example.com')
$HTTP_Response = $HTTP_Request.GetResponse()
$HTTP_Status = [int]$HTTP_Response.StatusCode

If ($HTTP_Status -eq 200) { 
    Write-Host "Site is OK!" 
} Else {
    Write-Host "The Site may be down, please check!"
}

$HTTP_Response.Close()
 

我得到了答复:

远程服务器返回错误:(401)未经授权.

但是在那之后我得到了:

网站还可以

这表示可以吗?如果没有,那是什么问题?

解决方案

您会好起来的,因为您重新运行了命令,并且$HTTP_Response包含上一次成功运行的对象.将try/catch与正则表达式结合使用以提取正确的状态代码(并清理变量):

 $HTTP_Response = $null
$HTTP_Request = [System.Net.WebRequest]::Create('http://example.com/')
try{
    $HTTP_Response = $HTTP_Request.GetResponse()
    $HTTP_Status = [int]$HTTP_Response.StatusCode

    If ($HTTP_Status -eq 200) { 
        Write-Host "Site is OK!" 
    }
    else{
        Write-Host ("Site might be OK, status code:" + $HTTP_Status)
    }
    $HTTP_Response.Close()
}
catch{
    $HTTP_Status = [regex]::matches($_.exception.message, "(?<=\()[\d]{3}").Value
    Write-Host ("There was an error, status code:" + $HTTP_Status)
}
 

.Net HttpWebRequest.GetResponse()在非OK状态代码上引发异常,您可以在此处了解更多信息:.Net HttpWebRequest.GetResponse()在返回HTTP状态码400(错误请求)时引发异常

I am trying to verify if my URLs get a response. in other words, i am trying to check if the authentication has succeeded approaching the site.

I used:

$HTTP_Request = [System.Net.WebRequest]::Create('http://example.com')
$HTTP_Response = $HTTP_Request.GetResponse()
$HTTP_Status = [int]$HTTP_Response.StatusCode

If ($HTTP_Status -eq 200) { 
    Write-Host "Site is OK!" 
} Else {
    Write-Host "The Site may be down, please check!"
}

$HTTP_Response.Close()

and I got the response:

The remote server returned an error: (401) Unauthorized.

but after that i got:

site is ok

Does that mean it's ok? If not, what is wrong?

解决方案

You are getting OK because you rerun your command and $HTTP_Response contains an object from a previous, successful run. Use try/catch combined with a regex to extract correct status code(and clean up your variables):

$HTTP_Response = $null
$HTTP_Request = [System.Net.WebRequest]::Create('http://example.com/')
try{
    $HTTP_Response = $HTTP_Request.GetResponse()
    $HTTP_Status = [int]$HTTP_Response.StatusCode

    If ($HTTP_Status -eq 200) { 
        Write-Host "Site is OK!" 
    }
    else{
        Write-Host ("Site might be OK, status code:" + $HTTP_Status)
    }
    $HTTP_Response.Close()
}
catch{
    $HTTP_Status = [regex]::matches($_.exception.message, "(?<=\()[\d]{3}").Value
    Write-Host ("There was an error, status code:" + $HTTP_Status)
}

.Net HttpWebRequest.GetResponse() throws exceptions on non-OK status codes, you can read more about it here: .Net HttpWebRequest.GetResponse() raises exception when http status code 400 (bad request) is returned

这篇关于&quot;远程服务器返回错误:(401)未经授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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