特定の Web サービスのエラー応答を解析するソリューションを探しています。以下のサンプルは一般的にうまく機能しますが、応答が 64kb を超える場合、コンテンツは例外でまったく利用できません。
ここで webHttpClient を使用して MaxResponseContentBufferSize を増やすことを推奨するソリューションを見てきましたが、特定の WebClient オブジェクトに対してこれを行うにはどうすればよいですか? 以下の TLS12 設定のように、すべてのネット Web コールに対してその BufferSize をグローバルに変更するオプションはありますか?
ここに私のサンプルコードがあります:
# 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"
}