Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7585787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:15:32+00:00 2026-05-30T19:15:32+00:00

I have a Powershell script that uses System.Net.HttpWebRequest to communicate with a remote host.

  • 0

I have a Powershell script that uses System.Net.HttpWebRequest to communicate with a remote host.

I create the request, set properties accordingly and call getresponse() and getresponsestream() to read the entire response from the server to a string. This works fine as long as the server responds with a “200 OK” message.

If the server responds with a “400 Bad Request” or any other error code, getresponse() and getresponsestream() throw exceptions and return nothing. My problem is there is more detailed error information included in the response header which I need so I can do my own error handling.

How would I be able to retrieve this 400 Bad Request signal?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:15:33+00:00Added an answer on May 30, 2026 at 7:15 pm

    Edit: I misunderstood the question at first, but it turns out that you can retrieve the response header by using the HttpWebResponse.GetResponseHeader() method. If an exception occurs, the HttpWebRequest.GetResponse() method returns $null, and you have to use this code to retrieve the HttpWebResponse object, so that you can call GetResponseHeader() on it:

    # If an exception occurs, get the HttpWebResponse object from the WebException object
    $HttpWebResponse = $Error[0].Exception.InnerException.Response;
    

    I’m pretty sure you’ll want to stick with the System.Net.HttpWebRequest instead of the System.Net.WebClient object. Here is an example, similar to what you probably already have:

    # Create a HttpWebRequest using the Create() static method
    $HttpWebRequest = [System.Net.HttpWebRequest]::Create("http://www.google.com/");
    
    # Get an HttpWebResponse object
    $HttpWebResponse = $HttpWebRequest.GetResponse();
    
    # Get the integer value of the HttpStatusCode enumeration
    Write-Host -Object $HttpWebResponse.StatusCode.value__;
    

    The GetResponse() method returns a HttpWebResponse object, which has a property named StatusCode, which points to a value in the HttpStatusCode .NET enumeration. Once you get a reference to the enumeration, we use the value__ property to get the integer that is associated with the returned enum value.

    If you get a null value from the GetResponse() method, then you’ll want to read the most current error message in your catch {..} block. The Exception.ErrorRecord property should be the most helpful.

    try {
      $HttpWebResponse = $null;
      $HttpWebRequest = [System.Net.HttpWebRequest]::Create("http://www.asdf.com/asdf");
      $HttpWebResponse = $HttpWebRequest.GetResponse();
      if ($HttpWebResponse) {
        Write-Host -Object $HttpWebResponse.StatusCode.value__;
        Write-Host -Object $HttpWebResponse.GetResponseHeader("X-Detailed-Error");
      }
    }
    catch {
      $ErrorMessage = $Error[0].Exception.ErrorRecord.Exception.Message;
      $Matched = ($ErrorMessage -match '[0-9]{3}')
      if ($Matched) {
        Write-Host -Object ('HTTP status code was {0} ({1})' -f $HttpStatusCode, $matches.0);
      }
      else {
        Write-Host -Object $ErrorMessage;
      }
    
      $HttpWebResponse = $Error[0].Exception.InnerException.Response;
      $HttpWebResponse.GetResponseHeader("X-Detailed-Error");
    }
    

    http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx

    http://msdn.microsoft.com/en-us/library/system.net.httpstatuscode.aspx

    http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple PowerShell script that uses WMI to create a web site
I have a Powershell script that uses this PowerShell module . It basically allows
I have a Powershell script that is loading a .NET assembly (.EXE in my
I have a PowerShell script that uses du.exe ( Disk Usage originally from Sysinternals)
Does anyone have a handy powershell script that gets a set of files from
I have written a PowerShell script that will create an email, however I can't
I have a Powershell script that copies files from one location to another. Once
I have a complex Powershell script that gets run as part of a SQL
I have a powershell script that runs fxcopcmd in the output window. Turns out
I have a Powershell script that reads values off of the pipeline: PARAM (

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.