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