Powershell - Invoke-WebRequest to a URL with literal '/' (/) in it

前端 未结 3 1394
半阙折子戏
半阙折子戏 2021-02-15 11:53

I have been trying to access a URL with a / character in it from powershell, using the following command (it\'s a query to a gitlab server to retrieve a project ca

3条回答
  •  走了就别回头了
    2021-02-15 12:37

    Try the URL through this function

    function fixuri($uri){
      $UnEscapeDotsAndSlashes = 0x2000000;
      $SimpleUserSyntax = 0x20000;
    
      $type = $uri.GetType();
      $fieldInfo = $type.GetField("m_Syntax", ([System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic));
    
      $uriParser = $fieldInfo.GetValue($uri);
      $typeUriParser = $uriParser.GetType().BaseType;
    $fieldInfo = $typeUriParser.GetField("m_Flags", ([System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic -bor [System.Reflection.BindingFlags]::FlattenHierarchy));
    $uriSyntaxFlags = $fieldInfo.GetValue($uriParser);
    
    $uriSyntaxFlags = $uriSyntaxFlags -band (-bnot $UnEscapeDotsAndSlashes);
    $uriSyntaxFlags = $uriSyntaxFlags -band (-bnot $SimpleUserSyntax);
    $fieldInfo.SetValue($uriParser, $uriSyntaxFlags);
    }
    
    $uri = New-Object System.Uri -ArgumentList ("https://server.com/api/v3/projects/foo%2Fbar")
    fixuri $uri
    

提交回复
热议问题