Powershell wget protocol violation

无人久伴 提交于 2021-02-04 17:38:21

问题


I am trying to browse a command on an embedded webserver using wget / invoke-webrequest. How can this error be avoided?

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

Already tried multiple things, for example below without success:

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

When using BITS instead, this is the error I'm getting

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

Thanks so much for your help Clem


回答1:


Setting the ServerCertificateValidationCallback delegate won't help you - SSL/TLS is not the protocol being referred to - the protocol violation is with regards to the HTTP headers (eg. long after TLS has been established).

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

Using reflection, it can also be set from the runtime. This Technet forum answer give's a great example of how to do so in PowerShell, we can wrap that in a nifty function like 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



来源:https://stackoverflow.com/questions/35260354/powershell-wget-protocol-violation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!