Mysticism: Invoke-WebRequest working only via ISE

后端 未结 1 1228
陌清茗
陌清茗 2021-01-21 04:40

I have killed 3 hours today and don\'t understand why?

I have easy script:

$user = \'icm\'
$pass = \'icm\'
$pair = \"$($user):$($pass)\"
$url = \'http://         


        
1条回答
  •  别那么骄傲
    2021-01-21 05:04

    I had also the same problem with RabbitMQ, the cause is the escaping of %2f in URL. See Percent-encoded slash (“/”) is decoded before the request dispatch

    With the tricks of the above answer, it works both in ISE and console :

    $urlFixSrc = @" 
    using System;
    using System.Reflection;
    
    public static class URLFix 
    { 
        public static void ForceCanonicalPathAndQuery(Uri uri)
        {
            string paq = uri.PathAndQuery;
            FieldInfo flagsFieldInfo = typeof(Uri).GetField("m_Flags", BindingFlags.Instance | BindingFlags.NonPublic);
            ulong flags = (ulong) flagsFieldInfo.GetValue(uri);
            flags &= ~((ulong) 0x30);
            flagsFieldInfo.SetValue(uri, flags);
        }
    } 
    "@ 
    Add-Type -TypeDefinition $urlFixSrc -Language CSharp
    
    $url = [URI]$url
    
    Invoke-WebRequest -Uri $url -Headers $headers -ContentType "application/json"
    

    0 讨论(0)
提交回复
热议问题