2021-06-01

PowerShell Invoke-WebRequest 出現錯誤: The server committed a protocol violation. Section=ResponseStatusLine

又踩到雷啦!

用 PowerShell Invoke-WebRequest 向某個網站發出 Request 的時候出現錯誤:

The server committed a protocol violation. Section=ResponseStatusLine


解決辦法:

在 Invoke-WebRequest 之前先執行以下 Script:

$netAssembly = [Reflection.Assembly]::GetAssembly([System.Net.Configuration.SettingsSection])
if ($netAssembly) {
$bindingFlags = [Reflection.BindingFlags] "Static,GetProperty,NonPublic";
$settingsType = $netAssembly.GetType("System.Net.Configuration.SettingsSectionInternal");
$instance = $settingsType.InvokeMember("Section", $bindingFlags, $null, $null, @());
if($instance) {
$bindingFlags = "NonPublic","Instance";
$useUnsafeHeaderParsingField = $settingsType.GetField("useUnsafeHeaderParsing", $bindingFlags);
if($useUnsafeHeaderParsingField) {
  $useUnsafeHeaderParsingField.SetValue($instance, $true);
};
};
};

跑完以上再下 Invoke-WebRequest URI $URL指令即可

沒有留言:

張貼留言