PowerShell
PowerShell

Reputation: 2051

Invoking Web Request in PowerShell Version 2.0 Timed out

This in continuation to one of my previous queries Invoke-WebRequest equivalent in PowerShell v2

Im trying to run the below script, it ran 2-3 timed but after that im repeatedly getting the below timed out error

Exception calling "GetRequestStream" with "0" argument(s): "The operation has timed out"

Request Timed Out Error

Here's the script

function Invoke_Workflow {

        param(
        [Parameter(Mandatory=$True)]
        [string]$arg,

        [Parameter(Mandatory=$True)]
        [string]$whostname,

        [Parameter(Mandatory=$True)]
        [string]$uuid,

        [Parameter(Mandatory=$True)]
        [string]$wfname,

        [Parameter(Mandatory=$True)]
        [string]$wpassword,

        [Parameter(Mandatory=$True)]
        [string]$wusername

        )


$body = 
"<wInput>
  <userInputValues>
   <userInputEntry value='$arg' key='stringArgument'/>
  </userInputValues>
  <executionDateAndTime></executionDateAndTime>
  <comments></comments> 
</wInput>"

# Disable certificate validation using certificate policy that ignores all certificates
add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;

    public class IDontCarePolicy : ICertificatePolicy {
        public IDontCarePolicy() {}
        public bool CheckValidationResult(
            ServicePoint sPoint, X509Certificate cert,
            WebRequest wRequest, int certProb) {
            return true;
        }
    }
"@
[System.Net.ServicePointManager]::CertificatePolicy = new-object IDontCarePolicy 


$password = ConvertTo-SecureString $wfapassword -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($wfausername, $password)

$request = [System.Net.WebRequest]::Create($URI1)
$request.ContentType = "application/xml"
$request.Method = "POST"
$request.Credentials = $credential

# $request | Get-Member  for a list of methods and properties 

try
{

$request.ServicePoint.MaxIdleTime = 5000;
$request.ServicePoint.ConnectionLeaseTimeout = 5000;
    $requestStream = $request.GetRequestStream()
    $streamWriter = New-Object System.IO.StreamWriter($requestStream)
    $streamWriter.Write($body)
}

finally
{
    if ($null -ne $streamWriter) { $streamWriter.Dispose() }
    if ($null -ne $requestStream) { $requestStream.Dispose() }
}

$res = $request.GetResponse()

$request.Abort()


}

Upvotes: 1

Views: 6337

Answers (1)

Anthony Bailey
Anthony Bailey

Reputation: 56

Problem is most likely related to the default Servicepoint connection limit which is 2.

You can increase it (10 as an example below) and if you are making many requests in a single powershell session then adding a check of the currentconnection count and clearing them (8 as an example below) prior to reaching the set connection limit:

$request.ServicePoint.ConnectionLimit =10;
$conn=$request.ServicePoint.CurrentConnections
    If ($conn -ge 8) {
    $request.ServicePoint.CloseConnectionGroup("")
}

Upvotes: 4

Related Questions