如何从PowerShell请求的404页面获得响应

Car*_*Dev 8 powershell teamcity

我必须调用TeamCity公开的API,告诉我用户是否存在.API网址是这样的:http://myteamcityserver.com:8080/httpAuth/app/rest/users/monkey

从浏览器(或fiddler)调用时,我得到以下内容:

Error has occurred during request processing (Not Found).
Error: jetbrains.buildServer.server.rest.errors.NotFoundException: No user can be found by username 'monkey'.
Could not find the entity requested. Check the reference is correct and the user has permissions to access the entity.
Run Code Online (Sandbox Code Playgroud)

我必须使用powershell调用API.当我这样做时,我得到一个例外,我没有看到上面的文字.这是我使用的powershell:

try{
    $client = New-Object System.Net.WebClient
    $client.Credentials = New-Object System.Net.NetworkCredential $TeamCityAgentUserName, $TeamCityAgentPassword
    $teamCityUser = $client.DownloadString($url)
    return $teamCityUser
}
catch
{
    $exceptionDetails = $_.Exception
    Write-Host "$exceptionDetails" -foregroundcolor "red"
}
Run Code Online (Sandbox Code Playgroud)

例外:

System.Management.Automation.MethodInvocationException: Exception calling "DownloadString" with "1" argument(s): "The remote server returned an error: (404) Not Found." ---> System.Net.WebException: The remote server returned an error: (404) Not Found.
   at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
   at System.Net.WebClient.DownloadString(Uri address)
   at CallSite.Target(Closure , CallSite , Object , Object )
   --- End of inner exception stack trace ---
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
Run Code Online (Sandbox Code Playgroud)

我需要能够检查返回的页面是否包含上述文本.这样我知道是否应该自动创建新用户.我可以检查404,但我担心的是,如果API被更改并且呼叫确实返回404,那么我就不会更聪明了.

Bro*_*ass 9

更改catch子句以捕获更具体的WebException,然后您可以使用Response它上面的属性来获取状态代码:

{
  #...
} 
catch [System.Net.WebException] 
{
    $statusCode = [int]$_.Exception.Response.StatusCode
    $html = $_.Exception.Response.StatusDescription
}
Run Code Online (Sandbox Code Playgroud)

  • 读完你指向我的stackoverflow帖子后,我得到了以下代码,这正是我需要的:catch [System.Net.WebException] {$ exception = $ _.Exception $ respstream = $ exception.Response.GetResponseStream( )$ sr = new-object System.IO.StreamReader $ respstream $ result = $ sr.ReadToEnd()write-host $ result} (3认同)

小智 -4

您可以尝试使用 Internet Explorer COM 对象。它允许您检查浏览器返回代码并导航 HTML 对象模型。

注意:我发现您需要从提升的 PowerShell 提示符运行此命令才能维护 COM 对象定义。

$url = "http://myteamcityserver.com:8080/httpAuth/app/rest/users/monkey"
$ie = New-Object -ComObject InternetExplorer.Application
Run Code Online (Sandbox Code Playgroud)

添加此以查看浏览器

$ie.visibility = $true
Run Code Online (Sandbox Code Playgroud)

导航至该网站

$ie.navigate($url)
Run Code Online (Sandbox Code Playgroud)

这将暂停脚本,直到页面完全加载

do { start-sleep -Milliseconds 250 } until ($ie.ReadyState -eq 4)
Run Code Online (Sandbox Code Playgroud)

然后验证您的 URL 以确保它不是错误页面

if ($ie.document.url -ne $url) { 
   Write-Host "Site Failed to Load" -ForegroundColor "RED"
} else {
   [Retrieve and Return Data]
}
Run Code Online (Sandbox Code Playgroud)

您可以通过 $ie.document 导航 HTML 对象模型。使用 Get-Member 和 HTML 方法,例如 GetElementsByTagName() 或 GetElementById()。

如果凭据是一个问题,请将其构建到函数中,然后使用 Invoke-Command 和 -Credentials 参数来定义您的登录信息。

  • 仅仅为了执行网络请求而启动 IE 听起来是一个非常糟糕的主意 (2认同)