Powershell - How to receive large response-headers from error-response of a web-request?

604 Views Asked by At

I am looking for a solution to parse an error-response of a given web-service. Below sample works great in general, but if the response is larger than 64kb then the content is not availabe in the exception at all.

I have seen some solutions recommending to use webHttpClient and increase the MaxResponseContentBufferSize here, but how can I do this for a given WebClient-object? Is there any option to change that BufferSize globally for all net-webcalls like below TLS12-settings?

Here is my sample-code:

# using net-webclient to use individual user-side proxy-settings:
$web = new-object Net.WebClient
[Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$url = "address to web-service"
try {
    $response = $web.DownloadString($url)
} catch [System.Net.WebException] {

    # this part needs to work even if the error-response in larger than 64kb
    # unfortunately the response-object is empty in such case

    $message = $_.Exception.Response
    $stream = $message.GetResponseStream()
    $reader = new-object System.IO.StreamReader ($stream)
    $body = $reader.ReadToEnd()
    write-host "#error:$body"
}
1

There are 1 best solutions below

0
Carsten On BEST ANSWER

I solved it at the end by switching to system.net.httpclient. That way I still repect any custom proxy-settings and also avoid the above mentioned 64kb-limit in any error-response. Here a sample how to use it:

$url  = "address to web-service"
$cred = Get-Credential

# define settings for the http-client:
Add-Type -AssemblyName System.Net.Http
$ignoreCerts = [System.Net.Http.HttpClientHandler]::DangerousAcceptAnyServerCertificateValidator
$handler = [System.Net.Http.HttpClientHandler]::new()
$handler.ServerCertificateCustomValidationCallback = $ignoreCerts
$handler.Credentials = $cred
$handler.PreAuthenticate = $true

$client = [System.Net.Http.HttpClient]::new($handler)
$client.Timeout = [System.TimeSpan]::FromSeconds(10)

$result = $client.GetAsync($url).result
$response = $result.Content.ReadAsStringAsync().Result
write-host $response