PowerShellでInvoke-RestMethodやInvoke-WebRequestなどを使って、リクエストしたとき、以下のような例外が発生したときの対処方法を記します。
Invoke-RestMethod : サーバーによってプロトコル違反が発生しました. Section=ResponseHeader Detail=CR の後には LF を指定しなければなりません。
リクエストから返却されたデータをInvoke-RestMethodやInvoke-WebRequestで検証するとHTTPプロトコル違反のため例外が発生していると考えられます。
以下のメッセージが表示されないように、プロトコル違反による例外を無視するように設定する手順を以下に記します。
Invoke-RestMethod : サーバーによってプロトコル違反が発生しました. Section=ResponseHeader Detail=CR の後には LF を指定しなければなりません。~
(本来、サーバー側で対処すべきですが…)
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net>
追記する場所は、<configuration>..</configuration>の中になります。
<configuration>
<uri>
<schemeSettings>
<add name="http" genericUriParserOptions="DontUnescapePathDotsAndSlashes"/>
<add name="https" genericUriParserOptions="DontUnescapePathDotsAndSlashes"/>
</schemeSettings>
</uri>
</configuration><configuration>
<uri>
<schemeSettings>
<add name="http" genericUriParserOptions="DontUnescapePathDotsAndSlashes"/>
<add name="https" genericUriParserOptions="DontUnescapePathDotsAndSlashes"/>
</schemeSettings>
</uri>
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net>
</configuration>尚、今回はpowershell.exe.configの修正でしたが、他のアプリケーションの場合やアプリケーション開発場合は、
アプリケーション.exe.configを修正することになります。