gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 05:25:17 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com