Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

system.Net.WebRequest - powershell - and 'The operation has timed out'

I need to ask a lot of url`s with powershell and check status code

$Request = $null
$Request= [system.Net.WebRequest]::Create($Url)
$Request.Timeout = 100
$Request.AllowAutoRedirect = $true

 try {

       $Response = $Request.GetResponse()

     } catch [System.Net.WebException] {

       $someexception   = $_.Exception.Message 
     }

     $http_status      =  [int]$res.StatusCode

    if ([int]$Response.StatusCode.value__ -ne 200) #если не 200 - алерт
    {
        $state       = 'ERROR'


    }
        else
    {
        $state = 'OK' 


    }

$Request.Close

Any way i am trying to use it on server - its ok via manual test, but if i run a lot of scripts (in M$ SCOM for monitoring) - SOME of them will fail with The operation has timed out

If i will check problem url - it is ok

Why it can time out in case lot of scripts in one time?how to debug it? Its is even http 200 from server logs ..

like image 743
Igor Kuznetsov Avatar asked Oct 15 '25 16:10

Igor Kuznetsov


2 Answers

[System.Net.ServicePointManager]::DefaultConnectionLimit = 1024 before the request creation solved the problem.

like image 97
Igor Kuznetsov Avatar answered Oct 17 '25 09:10

Igor Kuznetsov


The timeout property that you're setting to 100 is in milliseconds.

That's pretty short. Might want to set it to a few seconds.

It happens because the server side took longer than that amount of time to actually send the data. It may have sent a 200 response but the entire request did not complete in the time allotted. Definitely possible if you're sending a lot of requests at once.

like image 38
briantist Avatar answered Oct 17 '25 10:10

briantist



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!