gpt4 book ai didi

rest - Invoke-RestMethod - 忽略自签名证书

转载 作者:行者123 更新时间:2023-12-01 21:32:53 27 4
gpt4 key购买 nike

似乎这个问题已经被提出并得到解答,但到目前为止我遇到的每个解决方案都没有帮助。我正在编写一个 PowerShell 脚本来运行一些 REST API 来获取使用信息。我的脚本在尝试与服务器通信时立即中断。为了进行测试,我正在执行一个非常简单的命令:

Invoke-RestMethod 'https://server:4443/login'

返回时出现以下错误:

Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send.

我可以运行相同的命令,但使用 URL google.com,并且得到有效的返回,因此我知道该命令一般来说是有效的。

如果我在服务器本身上运行等效的curl,事情就会按预期完成。以下是curl 命令的详细输出的片段:

* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server key exchange (12):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSL connection using TLSv1.0 / DHE-RSA-AES256-SHA
* Server certificate:
* subject: CN=localhost
* start date: 2016-03-22 21:48:57 GMT
* expire date: 2026-03-20 21:48:57 GMT
* issuer: CN=localhost
* SSL certificate verify result: self signed certificate (18), continuing anyway.

我只是根据搜索 PowerShell 返回的相当通用的错误假设这是一个自签名证书问题。

我已经尝试过:

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

以及其他类似的方法(复杂函数)来帮助忽略证书问题,但运气不佳。

我正在运行 PowerShell 5,以防有帮助。

我对 PowerShell 代码很熟悉,但这是我第一次尝试 Invoke-RestMethod,所以也许我遗漏了一些东西。任何见解都值得赞赏。

最佳答案

这也适用于具有 invoke-restmethod/webrequest 的更高版本的 powershell。它通过将处理程序实现为 native .net 来避免对运行空间的需求:

if (-not("dummy" -as [type])) {
add-type -TypeDefinition @"
using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

public static class Dummy {
public static bool ReturnTrue(object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors) { return true; }

public static RemoteCertificateValidationCallback GetDelegate() {
return new RemoteCertificateValidationCallback(Dummy.ReturnTrue);
}
}
"@
}

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = [dummy]::GetDelegate()

希望这有帮助。

关于rest - Invoke-RestMethod - 忽略自签名证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36456104/

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