-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPSXmlRpcClient.psm1
363 lines (333 loc) · 21.4 KB
/
PSXmlRpcClient.psm1
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
function Invoke-XmlRpcRequest {
<#
.SYNOPSIS
发送XML-RPC请求
.DESCRIPTION
发送XML-RPC请求,如果服务器响应为200,则解析服务器响应;否则输出异常信息
.PARAMETER ServerUri
XML-RPC服务器地址
.PARAMETER MethodName
要调用的方法名称
.PARAMETER Params
要传递给方法的参数,可以为空
.INPUTS
uri
string
array
.OUTPUTS
object
.EXAMPLE
Invoke-XmlRpcRequest -Url "example.com" -MethodName "system.methodHelp" -Params "phone"
---------
Description
Calls a method "system.methodHelp("phone")" on the server example.com
#>
[CmdletBinding()]
[OutputType([object])]
param (
[Parameter(Mandatory=$true)]
[uri]$ServerUri,
[Parameter(Mandatory=$true)]
[string]$MethodName,
[array]$Params
)
$body=$(ConvertTo-XmlRpcPayload -MethodName $MethodName -Params $Params)
# ================================
# works well
$webRequest=[System.Net.HttpWebRequest]::CreateHttp($ServerUri)
$webRequest.ServicePoint.Expect100Continue=$false
$webRequest.AllowAutoRedirect=$true
$webRequest.KeepAlive=$false
$webRequest.ProtocolVersion=[System.Net.HttpVersion]::Version11
$webRequest.Method="POST"
$bytes=[System.Text.Encoding]::UTF8.GetBytes($body)
$req=$webRequest.GetRequestStream()
$req.Write($bytes, 0, $bytes.Length)
$req.Dispose()
$resp=[System.Net.HttpWebResponse]$webRequest.GetResponse()
$stream=$resp.GetResponseStream()
$sr=New-Object System.IO.StreamReader($stream)
$response=$sr.ReadToEnd()
$sr.Dispose()
$stream.Dispose()
$resp.Dispose()
# ================================
# 使用“2”个参数调用“UploadString”时发生异常:“服务器提交了协议冲突. Section=ResponseStatusLine”
# 有个比较奇怪的现象:
# 1、连续两次请求的第二次必出现该问题
# 2、与两次请求的先后顺序无关
# 3、连续四次请求的第二次、第四次必出现该问题
# $tmp=[System.Net.ServicePointManager]::Expect100Continue
# [System.Net.ServicePointManager]::Expect100Continue = $false
# $client = New-Object System.Net.WebClient
# try {
# $client.Encoding = [System.Text.Encoding]::UTF8
# $response = $client.UploadString($ServerUri, $body)
# }
# finally {
# [System.Net.ServicePointManager]::Expect100Continue=$tmp
# $client.Dispose()
# }
# Invoke-RestMethod : 417 - Expectation Failed
# $request=@{uri=$ServerUri;
# Method="POST";
# Headers=@{Authorization="Basic <base64-encoded-credentials>"; "Content-Type"="text/xml"}
# Body=$body}
# $response=$(Invoke-RestMethod -UseBasicParsing @request -DisableKeepAlive)
# Invoke-RestMethod : 417 - Expectation Failed
# $response=$(Invoke-RestMethod -Uri $ServerUri -UseBasicParsing -Method "POST" -Body $body)
# Invoke-WebRequest : 417 - Expectation Failed
# $response=$(Invoke-WebRequest -UseBasicParsing -Uri $ServerUri -DisableKeepAlive -Method "POST" -Body $body -ContentType "text/xml")
if ($response) {
ConvertFrom-Xml -Xml $response
}
}
function ConvertTo-XmlRpcPayload {
<#
.SYNOPSIS
将方法名和参数转换为XML-RPC格式的xml
.DESCRIPTION
将方法名和参数转换为XML-RPC格式的xml
.PARAMETER MethodName
要调用的方法名称
.PARAMETER Params
要传递给方法的参数,可以为空
.INPUTS
string
array
.OUTPUTS
string
.EXAMPLE
ConvertTo-XmlRpcPayload -MethodName "system.methodHelp" -Params "phone"
---------
Description
Convert method "system.methodHelp("phone")" to XML in XML-RPC request
#>
[CmdletBinding()]
[OutputType([string])]
param (
[Parameter(Mandatory=$true)]
[string]$MethodName,
[array]$Params
)
$requestXml="<?xml version='1.0'?><methodCall><methodName>{0}</methodName><params>{1}</params></methodCall>"
$paramsBuilder=[System.Text.StringBuilder]::new()
$Params | ForEach-Object {
if ($_) {
[void]$paramsBuilder.AppendFormat("<param><value>{0}</value></param>", $(ConvertTo-XmlRpcType -InputObject $_))
}
}
return $([xml]($requestXml -f $MethodName, $paramsBuilder.ToString())).OuterXml
}
function ConvertTo-XmlRpcType {
<#
.SYNOPSIS
将PowerShell类型转换为XML-RPC格式的xml
.DESCRIPTION
将PowerShell类型转换为XML-RPC格式的xml
.PARAMETER InputObject
PowerShell类型
.INPUTS
object
.OUTPUTS
string
.EXAMPLE
ConvertTo-XmlRpcType -InputObject 365
#>
[CmdletBinding()]
[OutputType([string])]
param (
$InputObject
)
begin {
# 在模块清单(psd1)文件中添加依赖在脚本中不好使???
# RequiredAssemblies = @("System.Web.dll")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Web")
}
process {
# four-byte signed integer
if ($InputObject -is [int]) {
# return "<i4>$InputObject</i4>"
return "<int>$InputObject</int>"
}
# 0 (false) or 1 (true)
if ($InputObject -is [bool]) {
if ($InputObject) { $boolValue=1 } else { $boolValue=0 }
return "<boolean>$boolValue</boolean>"
}
# string
if ($InputObject -is [string]) {
return "<string>$([System.Web.HttpUtility]::HtmlEncode($InputObject))</string>"
# return "<string>$InputObject</string>"
}
# double-precision signed floating point number
if (($InputObject -is [double]) -or ($InputObject -is [float]) -or ($InputObject -is [decimal])) {
return "<double>$InputObject</double>"
}
# date/time
if ($InputObject -is [datetime]) {
return "<dateTime.iso8601>$($InputObject.ToString('yyyyMMddTHH:mm:ss'))</dateTime.iso8601>"
}
# base64-encoded binary
if (($InputObject -is [array]) -and ($InputObject.Length -gt 0) -and ($InputObject[0] -is [byte])) {
return "<base64>$([System.Convert]::ToBase64String($InputObject))</base64>"
}
# <struct>s
if ($InputObject -is [hashtable]) {
$structBuilder=[System.Text.StringBuilder]::new()
[void]$structBuilder.Append("<struct>")
$InputObject.Keys | ForEach-Object {
[void]$structBuilder.AppendFormat("<member><name>{0}</name><value>{1}</value></member>", $_, $(ConvertTo-XmlRpcType -InputObject $InputObject[$_]))
}
[void]$structBuilder.Append("</struct>")
return $structBuilder.ToString()
}
# <array>s
if ($InputObject -is [array]) {
$arrayBuilder=[System.Text.StringBuilder]::new()
[void]$arrayBuilder.Append("<array><data>")
foreach ($obj in $InputObject) {
[void]$arrayBuilder.AppendFormat("<value>{0}</value>", $(ConvertTo-XmlRpcType -InputObject $obj))
}
[void]$arrayBuilder.Append("</data></array>")
return $arrayBuilder.ToString()
}
throw "$($InputObject.GetType().Name) type is not supported."
}
end {
}
}
function ConvertFrom-Xml {
<#
.SYNOPSIS
将XML-RPC服务器响应的xml转换为PowerShell类型
.DESCRIPTION
将XML-RPC服务器响应的xml转换为PowerShell类型
.PARAMETER Xml
XML-RPC格式的xml
.INPUTS
string or xml
.OUTPUTS
object
.EXAMPLE
ConvertFrom-Xml -Xml "<?xml version='1.0'?><methodResponse><params><param><value><string>South Dakota</string></value></param></params></methodResponse>"
#>
[CmdletBinding()]
[OutputType([object])]
param (
[Parameter(Mandatory=$true)]
$Xml
)
if ($Xml -is [string]) {
$XmlDocument=[xml]$Xml
} elseif ($Xml -is [xml]) {
$XmlDocument=$Xml
}
if ($XmlDocument -isnot [System.Xml.XmlDocument]) {
throw "Only types [string](xml format), [xml] are supported"
}
# normal response
foreach ($param in $XmlDocument.methodResponse.params.param) {
foreach ($value in $param.value) {
ConvertFrom-XmlRpcType -XmlObject $value.FirstChild
}
}
# fault response
foreach ($fault in $XmlDocument.methodResponse.fault) {
foreach ($value in $fault.value) {
ConvertFrom-XmlRpcType -XmlObject $value.struct
}
}
}
function ConvertFrom-XmlRpcType {
<#
.SYNOPSIS
将XML-RPC格式的xml转换为PowerShell类型
.DESCRIPTION
将XML-RPC格式的xml转换为PowerShell类型
.PARAMETER XmlObject
XML-RPC格式的xml
.INPUTS
string or xml
.OUTPUTS
int
bool
double
datetime
byte[]
hashtable
.EXAMPLE
ConvertFrom-XmlRpcType -XmlObject "<string>South Dakota</string>"
#>
[CmdletBinding()]
[OutputType([int])]
[OutputType([bool])]
[OutputType([double])]
[OutputType([datetime])]
[OutputType([byte[]])]
[OutputType([hashtable])]
param (
[Parameter(Mandatory=$true)]
$XmlObject
)
if ($XmlObject -is [string]) {
$XmlObject=$([xml]$XmlObject).DocumentElement
} elseif ($XmlObject -is [xml]) {
$XmlObject=$XmlObject.DocumentElement
}
if ($XmlObject -isnot [System.Xml.XmlElement]) {
throw "Only types [string](xml format), [xml], [System.Xml.XmlElement] are supported"
}
switch ($XmlObject.Name) {
# four-byte signed integer
"i4" {
[int]::Parse($XmlObject.InnerText)
break
}
"int" {
[int]::Parse($XmlObject.InnerText)
break
}
# 0 (false) or 1 (true)
"boolean" {
if ($XmlObject.InnerText -eq "1") {return $true} else {return $false}
break
}
# string
"string" {
$XmlObject.InnerText
break
}
# double-precision signed floating point number
"double" {
[double]::Parse($XmlObject.InnerText)
break
}
# date/time
"dateTime.iso8601" {
[datetime]::ParseExact($XmlObject.InnerText, "yyyyMMddTHH:mm:ss", [cultureinfo]::InvariantCulture)
break
}
# base64-encoded binary
"base64" {
[System.Convert]::FromBase64String($XmlObject.InnerText)
break
}
# <struct>s
"struct" {
$hashTable=@{}
$XmlObject.SelectNodes("member") | ForEach-Object {
$hashTable[$_.name] = $(ConvertFrom-XmlRpcType -XmlObject $_.value.FirstChild)
}
return $hashTable
break
}
# <array>s
"array" {
$XmlObject.SelectNodes("data/value") | ForEach-Object {
ConvertFrom-XmlRpcType -XmlObject $_.FirstChild
}
break
}
}
}