2016-06-11 19 views
12

Quando si invia la richiesta GET al server, che utilizza autofirmato certificato:Come analizzare JSON da Invoke-WebRequest in 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 
$RESPONSE=Invoke-WebRequest -Uri https://yadayada:8080/bla -Method GET 
echo $RESPONSE 

sto ottenendo seguente risposta:

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 contiene alcuni numeri cablate, così sono andato dopo RawContent come potrei analizzare il JSON all'interno, ignorando le intestazioni? o c'è un modo pulito per ottenere contenuti da quei numeri?

+2

Do yiou necessario il JSON a tutti o solo i valori? È possibile sostituire 'Invoke-WebRequest' con' Invoke-RestMethod' che converte automaticamente la risposta json in un 'psobject' in modo da poter usare' $ response = Intoke-RestMethod -Uri "https: // yadayada: 8080/bla"; $ response.flag' –

+1

Grazie Frode F., questo risolve il mio enigma. Per me i valori JSON sono abbastanza buoni, se vuoi ripubblicare questo come risposta, lo accetto – user52028778

risposta

21

È possibile sostituire Invoke-WebRequest con Invoke-RestMethod che auto-converte risposta JSON a una psobject in modo da poter utilizzare:

$response = Invoke-RestMethod -Uri "https://yadayada:8080/bla" 
$response.flag