首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Powershell v3 Invoke-WebRequest HTTPS错误

Powershell v3 Invoke-WebRequest HTTPS错误
EN

Stack Overflow用户
提问于 2012-07-28 07:39:41
回答 8查看 273.9K关注 0票数 137

使用Powershell v3的Invoke-WebRequest和Invoke-RestMethod,我已经成功地使用POST方法将json文件发布到https网站。

我使用的命令是

代码语言:javascript
运行
复制
 $cert=New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("cert.crt")
 Invoke-WebRequest -Uri https://IPADDRESS/resource -Credential $cred -certificate $cert -Body $json -ContentType application/json -Method POST

但是,当我尝试使用GET方法时:

代码语言:javascript
运行
复制
 Invoke-WebRequest -Uri https://IPADDRESS/resource -Credential $cred -certificate $cert -Method GET

返回以下错误

代码语言:javascript
运行
复制
 Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send.
 At line:8 char:11
 + $output = Invoke-RestMethod -Uri https://IPADDRESS/resource -Credential $cred
 +           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest)      [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

我尝试使用下面的代码来忽略SSL证书,但我不确定它是否真的在做什么。

代码语言:javascript
运行
复制
 [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

谁能提供一些指导,说明这里可能出了什么问题以及如何修复它?

谢谢

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2013-04-06 03:20:05

这个变通方法适用于我:http://connect.microsoft.com/PowerShell/feedback/details/419466/new-webserviceproxy-needs-force-parameter-to-ignore-ssl-errors

基本上,在您的PowerShell脚本中:

代码语言:javascript
运行
复制
add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
        public bool CheckValidationResult(
            ServicePoint srvPoint, X509Certificate certificate,
            WebRequest request, int certificateProblem) {
            return true;
        }
    }
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

$result = Invoke-WebRequest -Uri "https://IpAddress/resource"
票数 199
EN

Stack Overflow用户

发布于 2017-09-16 21:43:26

Lee的回答很好,但我也有关于web服务器支持的协议的问题。

在添加了以下几行之后,我可以获得https请求。正如在这个答案中指出的https://stackoverflow.com/a/36266735

代码语言:javascript
运行
复制
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols

我使用Lee的代码的完整解决方案。

代码语言:javascript
运行
复制
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
    public bool CheckValidationResult(
        ServicePoint srvPoint, X509Certificate certificate,
        WebRequest request, int certificateProblem) {
        return true;
    }
}
"@
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
票数 84
EN

Stack Overflow用户

发布于 2019-03-20 00:59:52

powershell的替代实现(没有c#源代码的Add-Type ):

代码语言:javascript
运行
复制
#requires -Version 5
#requires -PSEdition Desktop

class TrustAllCertsPolicy : System.Net.ICertificatePolicy {
    [bool] CheckValidationResult([System.Net.ServicePoint] $a,
                                 [System.Security.Cryptography.X509Certificates.X509Certificate] $b,
                                 [System.Net.WebRequest] $c,
                                 [int] $d) {
        return $true
    }
}
[System.Net.ServicePointManager]::CertificatePolicy = [TrustAllCertsPolicy]::new()
票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11696944

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档