PowerShell WebRequest POST

时间:2014-04-07 19:39:17

标签: powershell

在Windows PowerShell 3.0中引入了Invoke-RestMethod cmdlet。

Invoke-RestMethod cmdlet接受-Body<Object>参数来设置请求的正文。

由于某些限制Invoke-RestMethod在我们的案例中无法使用cmdlet。另一方面,文章InvokeRestMethod for the Rest of Us中描述的替代解决方案适合我们的需求:

$request = [System.Net.WebRequest]::Create($url)
$request.Method="Get"
$response = $request.GetResponse()
$requestStream = $response.GetResponseStream()
$readStream = New-Object System.IO.StreamReader $requestStream
$data=$readStream.ReadToEnd()
if($response.ContentType -match "application/xml") {
    $results = [xml]$data
} elseif($response.ContentType -match "application/json") {
    $results = $data | ConvertFrom-Json
} else {
    try {
        $results = [xml]$data
    } catch {
        $results = $data | ConvertFrom-Json
    }
}
$results 

但它仅适用于GET方法。 您能否建议如何扩展此代码示例,并能够使用POST方法发送请求正文(类似于Body中的Invoke-RestMethod参数)?

3 个答案:

答案 0 :(得分:17)

首先,更改更新HTTP方法的行。

$request.Method= 'POST';

接下来,您需要将消息正文添加到HttpWebRequest对象。为此,您需要获取对请求流的引用,然后向其中添加数据。

$Body = [byte[]][char[]]'asdf';
$Request = [System.Net.HttpWebRequest]::CreateHttp('http://www.mywebservicethatiwanttoquery.com/');
$Request.Method = 'POST';
$Stream = $Request.GetRequestStream();
$Stream.Write($Body, 0, $Body.Length);
$Request.GetResponse();

注意PowerShell Core版现已在GitHub上开源,在Linux,Mac和Windows上跨平台。应在此项目的GitHub问题跟踪器上报告Invoke-RestMethod cmdlet的任何问题,以便跟踪和修复它们。

答案 1 :(得分:4)

$myID = 666;
#the xml body should begin on column 1 no indentation.
$reqBody = @"
<?xml version="1.0" encoding="UTF-8"?>
<ns1:MyRequest
    xmlns:ns1="urn:com:foo:bar:v1"
    xmlns:ns2="urn:com:foo:xyz:v1"
    <ns2:MyID>$myID</ns2:MyID>
</ns13:MyRequest>
"@

Write-Host $reqBody;

try
{
    $endPoint = "http://myhost:80/myUri"
    Write-Host ("Querying "+$endPoint)
    $wr = [System.Net.HttpWebRequest]::Create($endPoint)
    $wr.Method= 'POST';
    $wr.ContentType="application/xml";
    $Body = [byte[]][char[]]$reqBody;
    $wr.Timeout = 10000;

    $Stream = $wr.GetRequestStream();

    $Stream.Write($Body, 0, $Body.Length);

    $Stream.Flush();
    $Stream.Close();

    $resp = $wr.GetResponse().GetResponseStream()

    $sr = New-Object System.IO.StreamReader($resp) 

    $respTxt = $sr.ReadToEnd()

    [System.Xml.XmlDocument] $result = $respTxt
    [String] $rs = $result.DocumentElement.OuterXml
    Write-Host "$($rs)";
}
catch
{
    $errorStatus = "Exception Message: " + $_.Exception.Message;
    Write-Host $errorStatus;
}

答案 2 :(得分:0)

感谢@Trevor Sullivan,它奏效了。我对上传 jsonstring 没有什么困惑。所以我在这里添加代码。

function Sendtodb {
$re = 0
try {
    $url = "http://localhost:804/api/yearend"
    $data = ' {
    "id": 1,
    "shopname": "robin",
    "location": "mlp",
    "phonenumber": "7012438494",
    "phonenumber2": "9645009584",
    "whodid": "asif"
}'



    $WebRequest = [System.Net.WebRequest]::Create($url)
    $WebRequest.Method = "POST"
    $Body = [byte[]][char[]]$data
    $WebRequest.ContentType = "application/json"
    $Stream = $WebRequest.GetRequestStream();
    $Stream.Write($Body, 0, $Body.Length);

    $Response = $WebRequest.GetResponse()
    $ResponseStream = $Response.GetResponseStream()
    $ReadStream = New-Object System.IO.StreamReader $ResponseStream
    $Data = $ReadStream.ReadToEnd()
    write-host $Response
    return 1
}
catch { retun false }
$re = 0

 }

  $dd = sendtodb