问题 PowerShell v2中的Invoke-WebRequest等效项


有人可以帮助我使用下面的cmdlet的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

10662
2017-08-04 14:10


起源



答案:


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

12
2017-08-04 14:56



嘿大卫,感谢您的回复,但我面临另一个问题,你可以帮助 stackoverflow.com/questions/25136833/... - PowerShell
如何在body参数中上传文件?我想上传apk文件。 - mashkurm


答案:


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

12
2017-08-04 14:56



嘿大卫,感谢您的回复,但我面临另一个问题,你可以帮助 stackoverflow.com/questions/25136833/... - PowerShell
如何在body参数中上传文件?我想上传apk文件。 - mashkurm


在这里,给它一个镜头。我提供了一些在线评论。最重要的是,你会想要使用 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);

2
2017-08-04 14:50



特雷弗你好,谢谢你,但是你能告诉我怎样才能通过证书? - PowerShell
当然,抱歉遗漏了这一部分。我更新了代码。 - Trevor Sullivan
嘿特雷弗感谢你,但我面临另一个问题,你能不能帮助你 stackoverflow.com/questions/25136833/... - PowerShell
您好,在这种情况下如何在body参数中发送apk文件? - mashkurm