从http请求到PowerShell

时间:2018-06-08 11:20:25

标签: powershell http

我需要使用PowerShell进行SOAP调用。 我是用SoapUI做的,它是这样的,它有效:

POST http://server/Api/Soap/ApiName.svc/basic HTTP/1.1
Accept-Encoding: gzip,deflate
User-Agent: UserAgent_Name
Content-Type: text/xml;charset=UTF-8
SOAPAction: "http://tempuri.org/ApiName/MethodName"
Authorization: eyJ0eXAiOiJKV1QiLCJhbGc
Content-Length: 240
Host: server:port
Connection: Keep-Alive

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"xmlns:tem="http://tempuri.org/">
  <soapenv:Header/>
<soapenv:Body>
   <tem:MethodName/>
</soapenv:Body>
</soapenv:Envelope>

我不知道如何将其翻译为PowerShell

1 个答案:

答案 0 :(得分:-2)

注意:这需要3.0版或更高版本的PowerShell

首先,您必须定义标题:

$headers = @{ "SOAPAction"="http://tempuri.org/ApiName/MethodName";
  "Authorization"="eyJ0eXAiOiJKV1QiLCJhbGc"
  }

接下来,身体:

$body = [xml]@'
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
 <soapenv:Header/>
<soapenv:Body>
   <tem:MethodName/>
</soapenv:Body>
</soapenv:Envelope>
'@

一旦定义了它,您就可以发送请求:

Invoke-WebRequest -User-Agent "UserAgent_Name" -Method Post -Headers $headers -Uri "http://server/Api/Soap/ApiName.svc/basic" -ContentType "text/xml;charset=`"utf-8`""

Source