rest - 使用自签名证书和基本身份验证的Powershell Invoke-RestMethod-是否有示例?

标签 rest powershell-3.0

我正在使用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 Get`

我无法使用Powershell V3的Invoke-RestMethod复制此文件,并且想知道是否有人可以共享示例代码来访问具有自签名证书并还使用基本授权的HTTPS Restful API。

我收到错误消息:
PS C:\Users\landg> C:\Users\landg\Documents\Scripts\CSA API\CSA_API_DEMO_take2.ps1
Invoke-RestMethod : Unable to connect to the remote server
At C:\Users\landg\Documents\Scripts\CSA API\CSA_API_DEMO_take2.ps1:31 char:1
+ Invoke-RestMethod -Uri https://192.168.5.3:8444/csa/rest/login/CSA-Provider/admi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

最佳答案

The source of my solution

使用它可以将我带到实际的服务器上。...现在出现HTTP 500错误,但是此错误又持续了一天。

这是我的“有效”代码段:

     add-type @"
        using System.Net;
        using System.Security.Cryptography.X509Certificates;

            public class IDontCarePolicy : ICertificatePolicy {
            public IDontCarePolicy() {}
            public bool CheckValidationResult(
                ServicePoint sPoint, X509Certificate cert,
                WebRequest wRequest, int certProb) {
                return true;
            }
        }
    "@
    [System.Net.ServicePointManager]::CertificatePolicy = new-object IDontCarePolicy 

    Invoke-RestMethod -Uri https://192.168.5.3:8444/csa/rest/login/CSA-Provider/admin -Headers @{"AUTHORIZATION"="Basic YWRtaW46Y2xvdWQ="} -Method Get

希望这可以在几个令人沮丧的时间里帮助其他人:)

关于rest - 使用自签名证书和基本身份验证的Powershell Invoke-RestMethod-是否有示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12187634/

相关文章:

Powershell 3克隆一个对象有意外的结果 - 影响其他对象

powershell - PowerShell Get-DiskUsage CmdLet:如何从其他驱动器/目录列出?

Powershell 4.0 Transcript 未捕获 Write-Host 语句的输出

java - Google App Engine 用户代理

javascript - X-WP-Nonce == Cookie 随机数无效

exception - 简单的 Try/catch 不起作用

rest - 让 Jersey 2 与嵌入式 Tomcat 一起工作的依赖项是什么

php - 错误 :org. apache.http.conn.HttpHostConnectException : Connection to http://10. 0.2.2:8080 被拒绝

Powershell 枚举属性的属性