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

我使用的命令是

 $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

但是,当我try 使用GET方法时,比如:

 Invoke-WebRequest -Uri https://IPADDRESS/resource -Credential $cred -certificate $cert -Method GET

返回以下错误

 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

我try 使用以下代码忽略SSL证书,但我不确定它是否真的有任何作用.

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

有人能提供一些关于这里可能出现的问题以及如何解决的指导吗?

谢谢

推荐答案

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

基本上,在PowerShell脚本中:

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"

.net相关问答推荐

Azure Function应用程序-如何升级.NET运行时

线程安全性的单元测试?

SetupSet() 已过时.代替什么?

在 C# 中输入按键

您是否使用 TestInitialize 或测试类构造函数来准备每个测试?为什么?

我可以使用 UriTemplate 将非字符串传递给 WCF RESTful 服务吗?

返回 IQueryable 或不返回 IQueryable

C# 中 try/finally 的开销?

带有输入字段的消息框

基于多个字符分隔符拆分字符串

有没有办法只在 C# 中设置一次属性

在 C# 中将匿名类型转换为键/值数组?

将 StreamReader 返回到开头

如何在 RabbitMQ 中设置重试次数?

在 WPF DataGrid 中绑定 ComboBoxColumn 的 ItemsSource

泛型类的默认构造函数的语法是什么?

任何人都知道缺少枚举通用约束的好方法吗?

多个列表与 IEnumerable.Intersect() 的交集

您可以将 Microsoft Entity Framework 与 Oracle 一起使用吗?

如何在我的机器上找到 fuslogvw.exe?