-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInvoke-RestMethodUTF8.ps1
54 lines (47 loc) · 1.89 KB
/
Invoke-RestMethodUTF8.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Function to help post HTTP request to web service
Function Invoke-RestMethodUTF8Post
{
param
(
[parameter(Mandatory = $True)][String]$Uri,
[ValidateScript({$_.GetType().Name -in 'XmlDocument', 'XmlElement', 'String'})]$Body,
[switch]$UseDefaultCredentials,
[int] $TimeoutSec = -1
)
if ($Body.GetType().Name -in 'XmlDocument', 'XmlElement')
{
$Body = $Body.InnerXml
}
$Buffer = [System.Text.Encoding]::UTF8.GetBytes($Body)
[System.Net.HttpWebRequest] $WebRequest = [System.Net.WebRequest]::Create($Uri)
if ($TimeoutSec -ge 0)
{
$TimeoutSec = $TimeoutSec * 1000
$WebRequest.Timeout = $TimeoutSec
}
$WebRequest.Method = 'POST'
$WebRequest.ContentType = 'application/x-www-form-urlencoded'
$WebRequest.ContentLength = $Buffer.Length
if ($UseDefaultCredentials)
{
$WebRequest.UseDefaultCredentials = $True
}
$RequestStream = $WebRequest.GetRequestStream()
$RequestStream.Write($Buffer, 0, $Buffer.Length)
$RequestStream.Flush()
$RequestStream.Close()
[System.Net.HttpWebResponse] $WebResponse = $WebRequest.GetResponse()
Write-Verbose -Message ('WebResponse: ' + ($WebResponse | Out-String))
$streamReader = New-Object -TypeName System.IO.StreamReader -ArgumentList ($WebResponse.GetResponseStream())
$Result = $streamReader.ReadToEnd()
if ($Result.StartsWith('<?xml version="1.0"'))
{
$Result = [xml]$Result
}
return $Result
}
$Url = 'http://example.com:80'
[Xml]$Xml = '<?xml version="1.0" encoding="UTF-8"?><example><example2>example3</example2></example>'
$Result = Invoke-RestMethodUTF8Post -Uri $Url -Body $xml -Verbose
$Result
#$Result.InnerXml