PowerShell and XMLRPC (PoSH Challenge part 12)
This level was another "Interesting" task, It could have been solved quick and easy ... was it not for one little problem ....
Level 13 involved speaking to PHP web service with XMLRPC, I search for PowerShell and XMLRPC got me here :
http://d.hatena.ne.jp/coma2n/20071125/1195991850
Seemed just what I needed, so I downloaded and tested it. But I not could get it working, I got a strange error back ( More about that later ).
So I did search a bit more and did find 2 Libraries in C# that did look useful, The first one did not work for me so I started working on the second one :
http://www.c-sharpcorner.com/UploadFile/nicholaschristopher/XmlRPC11282005013635AM/XmlRPC.aspx
After some time playing with it (and ranting about it to Jaykul on IRC) , On eof those reasons being that the library was not yet compiled and the provided NANT did not work (missong my documents or something) I had to load the code into VS2005 to compile it, but after working that out, I got the DLL and could load it in :
PoSH> [System.Reflection.Assembly]::LoadFile( "c:\PowerShell\bin\XmlRpc.dll")
GAC Version Location
--- ------- --------
False v2.0.50727 c:\PowerShell\bin\XmlRpc.dll
PoSH> $r = New-Object Nwc.XmlRpc.XmlRpcRequest
PoSH>
So I was "On the Road" again, but when I did think I was almost there, again I got an error back:
PoSH> $r.Send('http://www.pythonchallenge.com/pc/phonebook.php')
Exception calling "Send" with "1" argument(s): "The remote server returned an error: (417) Expectation Failed."
At line:1 char:8
+ $r.Send <<<< ('http://www.pythonchallenge.com/pc/phonebook.php')
PoSH>
Hmm, this did look very familiar ... it was the same error I got with the PowerShell script I got from the Japanese blog.
After more searching I found I needed a special flag for this in the app.config.
But as I was working from PowerShell I needed to make one for PowerShell, I task I had at hand before here : Language translation in PowerShell using PowerTab Part 1 when I did need the app.config to set httpWebRequest useUnsafeHeaderParsing = "true" to be able to use Babelfish
So I made this config file that needs to be placed in the same directory as PowerShell.exe ( $PSHome )
PowerShell.exe.config
<configuration>
<system.net>
<settings>
<servicePointManager expect100Continue="false" />
</settings>
</system.net>
</configuration>
After Placing the fiel above in the PowerShell direcotry , I started a new PowerShell session and did paste in the same code again :
PoSH> [System.Reflection.Assembly]::LoadFile( "c:\PowerShell\bin\XmlRpc.dll")
GAC Version Location
--- ------- --------
False v2.0.50727 c:\PowerShell\bin\XmlRpc.dll
PoSH> $r = New-Object Nwc.XmlRpc.XmlRpcRequest
PoSH> $r.MethodName = 'phone'
PoSH> $r.Params.Add("Bert")
0
PoSH> $r.Send('http://www.pythonchallenge.com/pc/phonebook.php')
Value FaultCode FaultString IsFault
----- --------- ----------- -------
555-XXXXXX 0 False
PoSH>
Great, this time it works !
But after that hack, I did think about the first PowerShell script I found ...
PoSH> (c:\PowerShell\PoshChallenge\get-xmlrpc.ps1 "http://www.pythonchallenge.com/pc/phonebook.php" phone @("Bert"))
xml methodResponse
--- --------------
version="1.0" methodResponse
PoSH> (c:\PowerShell\PoshChallenge\get-xmlrpc.ps1 "http://www.pythonchallenge.com/pc/phonebook.php" phone @("Bert")).met
hodResponse.params.param.value
string
------
555-XXXXX
PoSH>
And Yes this did work also now.
Oops finding the workaround for the (417) Expectation Failed error could have saved me hours, and all the trouble with gettting the library to work) and would have given me a "PowerShell Only" solution but as I was not used to this kind of work, this journey was a good learning source again.
The two Solutions, one with external library and one with the PowerShell only script from : http://d.hatena.ne.jp/coma2n/20071125/1195991850 ( Great Script, Thanks from here ! )
# XMLRPC from PowerShell
# http://www.c-sharpcorner.com/UploadFile/nicholaschristopher/XmlRPC11282005013635AM/XmlRPC.aspx
[System.Reflection.Assembly]::LoadFile( "c:\PowerShell\bin\XmlRpc.dll")
$r = New-Object Nwc.XmlRpc.XmlRpcRequest
$r.MethodName = 'phone'
$r.Params.Add("Bert")
$r.Send('http://www.pythonchallenge.com/pc/phonebook.php')
# http://d.hatena.ne.jp/coma2n/20071125/1195991850
(c:\PowerShell\PoshChallenge\get-xmlrpc.ps1 "http://www.pythonchallenge.com/pc/phonebook.php" phone @("Bert")).methodResponse.params.param.value
Great to see there was a PowerSHell solution available for XMLRPC in the community.
More to come , but also much that is there already ...
Enjoy,
Greetings /\/\o\/\/