【问题标题】:PowerShell WebRequest POSTPowerShell WebRequest POST
【发布时间】:2014-04-07 19:39:17
【问题描述】:

在 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 方法发送请求正文(类似于Invoke-RestMethod 中的Body 参数)?

【问题讨论】:

    标签: powershell


    【解决方案1】:

    首先,更改更新 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 上跨平台。 Invoke-RestMethod cmdlet 的任何问题都应在此项目的 GitHub 问题跟踪器上报告,以便跟踪和修复。

    【讨论】:

    • 谢谢你,特雷弗!这是我认为应该实施的方式,但不确定这是最好的方式
    • @TrevorSullivan 如果我有一个 json,身体会是什么样子?
    • @campinho:只需将“asdf”替换为您的 JSON。 :)
    • @TrevorSullivan 我可以这样发送文件吗?就像$Body = [reports[]]$filecontent; 我有一个问题here 现在我来了这个答案。你觉得这个解决方案在我的情况下如何?
    • @irl_irl 是的,我得到了这个问题的答案here
    【解决方案2】:
    $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;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-11-09
      • 2021-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多