Need help with System.Net.WebRequest in PowerShell

Welcome Forums General PowerShell Q&A Need help with System.Net.WebRequest in PowerShell

This topic contains 3 replies, has 2 voices, and was last updated by

1 year, 8 months ago.

  • Author
  • #68805

    Points: 0
    Rank: Member

    Hey Everyone!

    I understand I can use Invoke-WebRequest for what I am doing but I am trying to use this class and build the request. Unfortunately I haven't had any success to get it to work.

    I am trying to invoke a RabbitMQ API call. My code is below.

    try {
        $webrequest = [System.Net.WebRequest]::Create("http://RabbitMQ/api/nodes");
        $webrequest.Method = "GET"
        $webrequest.PreAuthenticate = $true
        $webrequest.Headers.Add("Authorization", "Basic " + "dGVzdDp0ZXN0")
        $webrequest.ContentType = "application/json"
        $response = $webrequest.GetResponse()
    catch [System.Net.WebException] {
        $Request = $_.Exception
        Write-host "Exception caught: $Request"

    The error I get back is that the operation timed out. I know the site is running as I can hit the endpoint myself on my browser. Is there something I am doing incorrect?

  • #68808

    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    Gosh I hope that's not a real password.

    WebRequest isn't a drop-in replacement for a browser. It's possible the server is looking for something else in the HTTP headers, that you haven't provided. But you're also saying it's a JSON content body, but you're doing a GET, which doesn't have a BODY. So it's possible you're confusing it. I don't know enough about RabbitMQ to tell you what it expects.

  • #68809

    Points: 0
    Rank: Member

    Hi there Don,

    First and most importantly that is not a real password. That is not even the correct URL. I always modify my code before I post it here. I try my best not to paste real endpoints/passwords/etc. 🙂 Thank you for pointing that out though.

    I can use the Invoke-WebRequest cmdlet and pass in the URL & Password as a PSCredential and Method=GET and everything works fine. However trying to build the request using this class I have to inject the username/password into the header.

    I can also run this URL via Fiddler and I pass in the Authorization and Content-Type into the Header and it works. Ill keep digging but thank you for your thoughts. If you have any more, please share them.


  • #68863

    Points: 0
    Rank: Member

    Update: I got this working.

    I ended up using the System.Net.WebClient Class downloadstring() method. I was making a RabbitMQ API Web call to retrieve some data on my Cluster environment. Code snippet below.

    $url = 
    $client = New-Object System.Net.WebClient;
    $client.headers["Authorization"] = "BASIC dGVzdDp0ZXN0"   ### Your password is Base64 Encoded
    	    $result = $client.DownloadString($url)
    	catch [System.Net.WebException]
    	    $result = $_.Exception.Response

The topic ‘Need help with System.Net.WebRequest in PowerShell’ is closed to new replies.