Invoke-WebRequest equivalent in PowerShell v2

后端 未结 2 1251
梦谈多话
梦谈多话 2020-12-10 12:21

Can some one help me with the powershell v2 version of the below cmdlet.

$body = 
\"
  
   

        
2条回答
  •  难免孤独
    2020-12-10 12:47

    Here, give this a shot. I provided some in-line comments. Bottom line, you're going to want to use the HttpWebRequest class from the .NET Base Class Library (BCL) to achieve what you're after.

    $Body = @"
    
      
       
      
      
       
    
    "@;
    
    # 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);
    

提交回复
热议问题