小编Gra*_*zer的帖子

Powershell Invoke-RestMethod使用自签名证书和基本身份验证 - 任何示例?

我正在使用RESTful API并使用Firefox RESTClient插件一切都很好.我可以轻松查询API.

但是,当我尝试将相同的API调用插入powershell时,它不起作用!

我已经尝试了以下代码来自其他各种帖子,这些帖子应该排除证书问题,但我仍然无法使其工作:

    # This nugget should help me to get around any self-signed certificate issues I believe

    $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)
            }
        }
    }

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

    $headers = @{"AUTHORIZATION"="Basic YWRtaW46Y2xvdWQ="}

    # I exported this certificate from the web browser
    $cert=New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("HPCSA.crt")

    Invoke-RestMethod -Uri https://192.168.5.3:8444/csa/rest/login/CSA-Provider/admin -Certificate $cert -Headers $headers -Method …
Run Code Online (Sandbox Code Playgroud)

rest powershell-3.0

8
推荐指数
2
解决办法
1万
查看次数

标签 统计

powershell-3.0 ×1

rest ×1