Powershell wget protocol violation

I am trying to view a command on an embedded web server using wget / invoke-webrequest. How to avoid this error?

wget: the server committed a protocol violation. Section = ResponseHeader Detail = CR must be followed by LF

Already tried several things, for example below, without success:

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true} 

When using BITS instead, this is the error I get

start-bitstransfer: the server did not return the file size. The URL may point to dynamic content. The Content-Length header is not available in the HTTP server response.

Thanks so much for your help Clem

+5
source share
1 answer

Setting the ServerCertificateValidationCallback delegate ServerCertificateValidationCallback not help you - SSL / TLS is not protocol - protocol violation is related to HTTP headers (for example, a long time after TLS has been set).

There is a .NET useUnsafeHeaderParsing .NET configuration flag that controls whether such violations are ignored or not.

Using reflection, it can also be set from the runtime. This Technet forum answer gives a great example of how to do this in PowerShell, we can wrap this in a great function, as shown below:

 function Set-UseUnsafeHeaderParsing { param( [Parameter(Mandatory,ParameterSetName='Enable')] [switch]$Enable, [Parameter(Mandatory,ParameterSetName='Disable')] [switch]$Disable ) $ShouldEnable = $PSCmdlet.ParameterSetName -eq 'Enable' $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, $ShouldEnable) } } } } 

And then use like:

 Set-UseUnsafeHeaderParsing -Enable 

before calling Invoke-WebRequest

+6
source

Source: https://habr.com/ru/post/1242500/


All Articles