向服务器发送 GET 请求时,使用自签名证书:
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
$RESPONSE=Invoke-WebRequest -Uri https://yadayada:8080/bla -Method GET
echo $RESPONSE
我收到以下回复:
StatusCode : 200
StatusDescription : OK
Content : {123, 10, 108, 111...}
RawContent : HTTP/1.1 200 OK
Content-Length: 21
Date: Sat, 11 Jun 2016 10:11:03 GMT
{
flag:false
}
Headers : {[Content-Length, 21], [Date, Sat, 11 Jun 2016 10:11:03 GMT]}
RawContentLength : 21
Content 包含一些有线数字,所以我寻找 RawContent,我将如何解析其中的 JSON,忽略 header ?或者有没有一种干净的方法可以从这些数字中获取内容?
最佳答案
您可以将 Invoke-WebRequest
替换为 Invoke-RestMethod
,它会自动将 json 响应转换为 psobject
,以便您可以使用:
$response = Invoke-RestMethod -Uri "https://yadayada:8080/bla"
$response.flag
关于json - 如何在 PowerShell 中解析来自 Invoke-WebRequest 的 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37762615/