title: PowerShell WebRequest : An Unexpected error occured on a send
body: <p>I have a similar question realted to this post</p>
  
  <p><a href="https://stackoverflow.com/questions/22627977/the-underlying-connection-was-closed-an-unexpected-error-occurred-on-a-send">An Unexpected error occured on a send</a></p>
  
  <p>My issue is slightly different where I'm actually getting a response back from the server on a windows server 2012 R2, but my local machine I'm getting</p>
  
  <blockquote>
    <p>The underlying connection was closed: An unexpected error occurred on a send.</p>
  </blockquote>
  
  <p>I understand that the server is ending the connection or the timeout is too low is the prescribed reason via Microsoft. This is the same script on 2 different machines one working, one not. My local machine is what is the error-thrower.</p>
  
  <p>Obviously this is not a direct issue with my script rather my localhost so I'm hoping someone could provide some insight as to why this works on a server host and not my laptop on the same domain?  </p>
  
  <p>If I use the try/catch on my local, all I see is this error. If I remove the try catch, I get the full response</p>
  
  <pre><code>Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
  At C:\blah some directory Examples\blah\myscript.ps1:82 char:17
  +     $response = Invoke-WebRequest -Headers $headers -Method GET -ContentType "ap ...
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
  + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
  
  id: 67kjadsfj5029838k3ka70387fe400
  state: active
  [I get a response]
  </code></pre>
  
  <p>So I'm actually getting a response back from the host with the requested data so I have to take the invokation out of the try/catch on my local to get it.  Obviously not ideal, I'd prefer to keep the script in-tact</p>
  
  <p>Ultimately, I am hoping if someone could provide some insight as to why powershell on my local machine is throwing this error and yet giving me the response, how do I suppress or fix it? If I could get the why and how that would be great but I'd settle for how at this point.</p>