1

I'm using a standard [System.Net.WebRequest] class to return the HTTP response code of a given URL.

The URL points to an internal web application server, which returns a "401 Unauthorised" error. This is actually OK as the service account running the script doesn't have a valid account to the application. However, I am more interested that the website is responding. However, I assumed that this is a HTTP Response in itself so I could manage this, but instead it returned as a null value.

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

Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (407) Proxy Authentication Required."

(I'm using Google in this example, which our servers are blocked from accessing external sites).

So I can't get as far as $HTTP_Status = [int]$HTTP_Response.StatusCode in the code because it won't accept 400 errors as a code.

How can I accept a 401 (or 407 in this example) in the query?

Thanks

6
  • So if i understand it correct you can't reach the statuscode because of the 401 Unauthrised? Is that what you are saying? I think this is by design. The system redirects you to the 401 page which contains only limited data. (don't quote me on that tho). Having said that, seeing that the system did try to verify your credentials, you would conclude that the server is still online and active. Commented Sep 28, 2017 at 13:36
  • .net is 'helping' you by proxy/generating an error when your web call returns an error status. You have to wrap your code in a try catch to continue processing once the error has been generated. Commented Sep 28, 2017 at 14:18
  • Snak3d0c - yes that's exactly what I am trying to say, and yes, the fact that it has processed the credentials suggests that the website is in fact responding, but I'm not getting a 200 OK. The URL takes me to the /login.aspx screen, but it auto-logs in for some reason that I don't understand, and therefore gives me the 401 error Commented Sep 28, 2017 at 14:46
  • Very strange. I'm just guessing here but do you have some cached passwords / credentials in the browser or something? Maybe try to clear those? If you are not doing a post somewhere and filling in the inputfields, it must get its data from somewhere right? Commented Sep 28, 2017 at 15:02
  • I know how to Try/Catch but how would I get the response code after that? Commented Sep 28, 2017 at 15:06

2 Answers 2

3

Got it!

try{
    $request = $null
    $request = Invoke-WebRequest -Uri "<URL>"
    } 
catch
    {              
     $request = $_.Exception.Response            
    }  
    $StatusCode = [int] $request.StatusCode;
    $StatusDescription = $request.StatusDescription;
Sign up to request clarification or add additional context in comments.

Comments

0

You could've done:

$HTTP_Request = [System.Net.WebRequest]::Create('http://google.com')
$HTTP_Request.Method = "GET"
$HTTP_Request.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials

[System.Net.HttpWebResponse]$HTTP_Response = $HTTP_Request.GetResponse()
Try {
    $HTTP_Status = [int]$HTTP_Response.StatusCode
}
Catch {
    #handle the error if you like, or not...
}

If ($HTTP_Status -eq 200) {
    Write-Host "Good Response"
} Else {
    Write-Host "Site Down or Access Denied"
}
# If you got a 404 Not Found, the response will be null so can't be closed
If ($HTTP_Response -eq $null) { } Else { $HTTP_Response.Close() }

You were missing the authentication piece:

$HTTP_Request.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials

I re-read your post and because your service account did not have access to the URL you are hitting (which was not actually google.com... you should've put myserver.com...grr), you would never actually get a 200, but always will get an exception. This is a bad practice, because instead of looking for the 200, you would have to always look for the 401 or 407 Unauthorized exception status code specifically, and if the code/response changed to something else, only then is it considered a failure - but technically it always had been a failure, because it never reached the site! It masks potential issues if you intend to go on deliberately using a site your service account doesn't have access to reach. And if your service account was ever granted access, your code would have to be updated.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.