Подтвердить что ты не робот

Эквивалент Invoke-WebRequest в PowerShell v2

Может ли кто-нибудь помочь мне с версией командной строки powershell версии 2 ниже.

$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
4b9b3361

Ответ 1

$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()

Ответ 2

Здесь сделайте это. Я представил некоторые комментарии в режиме онлайн. В нижней части вы хотите использовать класс HttpWebRequest из библиотеки базового класса.NET(BCL) для достижения того, что вам нужно.

$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);