Private/request.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function request( [string]$Url, [int]$Timeout, $Options ) {
    if ([string]::IsNullOrWhiteSpace($url)) {throw 'The URL is empty'}
    $request = [System.Net.WebRequest]::Create($Url)
    if ($Timeout)  { $request.Timeout = $Timeout*1000 }
 
    if ($Options.Headers) { 
        $Options.Headers.Keys | % { 
            if ([System.Net.WebHeaderCollection]::IsRestricted($_)) {
                $key = $_.Replace('-','')
                $request.$key = $Options.Headers[$_]
            }
            else { 
                $request.Headers.add($_, $Options.Headers[$_])
            }
        }
    }
    
    $response = $request.GetResponse()
    $response.Close()
    $response
}