在PowerShell v2中,等价于Invoke-WebRequest的命令是什么?

17

有人能帮我将下面的命令转成PowerShell v2版本吗?

$body = 
"<wInput>
  <uInputValues>
   <uInputEntry value='$arg' key='stringArgument'/>
  </uInputValues>
  <eDateAndTime></eDateAndTime>
  <comments></comments> 
</wInput>"

$password = ConvertTo-SecureString $wpassword -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($wusername, $password)

$output = Invoke-WebRequest -Uri $URI1 -Credential $credential -Method Post -ContentType application/xml -Body $body
3个回答

16
$URI1 = "<your uri>"

$password = ConvertTo-SecureString $wpassword -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($wusername, $password)

$request = [System.Net.WebRequest]::Create($URI1)
$request.ContentType = "application/xml"
$request.Method = "POST"
$request.Credentials = $credential

# $request | Get-Member  for a list of methods and properties 

try
{
    $requestStream = $request.GetRequestStream()
    $streamWriter = New-Object System.IO.StreamWriter($requestStream)
    $streamWriter.Write($body)
}

finally
{
    if ($null -ne $streamWriter) { $streamWriter.Dispose() }
    if ($null -ne $requestStream) { $requestStream.Dispose() }
}

$res = $request.GetResponse()

嗨,David,谢谢你的回复,但我遇到了另一个问题,你能帮忙吗?http://stackoverflow.com/questions/25136833/invoking-web-request-in-powershell-version-2-0-timed-out - PowerShell
如何在请求体参数中上传文件?我想上传apk文件。 - thisisdude

2

这里,试试这个。我提供了一些内联注释。总的来说,你需要使用.NET基础类库(BCL)中的HttpWebRequest类来实现你想要的功能。

$Body = @"
<wInput>
  <uInputValues>
   <uInputEntry value='$arg' key='stringArgument'/>
  </uInputValues>
  <eDateAndTime></eDateAndTime>
  <comments></comments> 
</wInput>
"@;

# Convert the message body to a byte array
$BodyBytes = [System.Text.Encoding]::UTF8.GetBytes($Body);
# Set the URI of the web service
$URI = [System.Uri]'http://www.google.com';

# Create a new web request
$WebRequest = [System.Net.HttpWebRequest]::CreateHttp($URI);
# Set the HTTP method
$WebRequest.Method = 'POST';
# Set the MIME type
$WebRequest.ContentType = 'application/xml';
# Set the credential for the web service
$WebRequest.Credentials = Get-Credential;
# Write the message body to the request stream
$WebRequest.GetRequestStream().Write($BodyBytes, 0, $BodyBytes.Length);

你好Trevor,谢谢你的回复。但是你能告诉我如何传递凭据吗? - PowerShell
当然,很抱歉错过了那部分。我已经更新了代码。 - user189198
嗨Trevor,谢谢你的帮助,但我又遇到了另一个问题,你能帮忙吗?http://stackoverflow.com/questions/25136833/invoking-web-request-in-powershell-version-2-0-timed-out - PowerShell
你好,请问在这种情况下我该如何将apk文件作为body参数发送? - thisisdude

1

这个方法下载二进制内容:

# PowerShell 2 version
$WebRequest=New-Object System.Net.WebClient
$WebRequest.UseDefaultCredentials=$true
#$WebRequest.Credentials=(Get-Credential)
$Data=$WebRequest.DownloadData("http://<url>")
[System.IO.File]::WriteAllBytes("<full path of file>",$Data)

# PowerShell 5 version
Invoke-WebRequest -Uri "http://<url>" -OutFile "<full path of file>" -UseDefaultCredentials -ContentType 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接