C# Call xml-rpc API [CS_xml-rpc_client] (GOOGLE: c# xml-rpc)
資料來源: https://stackoverflow.com/questions/40647779/how-to-call-an-api-which-is-based-on-xml-rpc-specification-in-c
GITHUB: https://github.com/jash-git/CS_xml-rpc_client
Code
01.xmlrpc_server.php
getParam(0); $x = $xval->scalarval(); $yval = $params->getParam(1); $y = $yval->scalarval(); // Build our response. $struct = array('sum' => new xmlrpcval($x + $y, 'int'), 'difference' => new xmlrpcval($x - $y, 'int')); return new xmlrpcresp(new xmlrpcval($struct, 'struct')); } // Declare our signature and provide some documentation. // (The PHP server supports remote introspection. Nifty!) $sumAndDifference_sig = array(array('struct', 'int', 'int')); $sumAndDifference_doc = 'Add and subtract two numbers'; new xmlrpc_server(array('sample.sumAndDifference' => array('function' => 'sumAndDifference', 'signature' => $sumAndDifference_sig, 'docstring' => $sumAndDifference_doc))); ?>
02.xmlrpc_client.php
XML-RPC PHP Demo XML-RPC PHP Demo
send($message); // Process the response. if (!$result) { print "Could not connect to HTTP server.
"; } elseif ($result->faultCode()) { print "XML-RPC Fault #" . $result->faultCode() . ": " . $result->faultString(); } else { $struct = $result->value(); $sumval = $struct->structmem('sum'); $sum = $sumval->scalarval(); $differenceval = $struct->structmem('difference'); $difference = $differenceval->scalarval(); print "
Sum: " . htmlentities($sum) . ", Difference: " . htmlentities($difference) . "
"; } ?> © 2021 GitHub, Inc.
03.C#
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using CookComputing.XmlRpc;//Step 2 ~ xml-rpc /* GOOGLE: c# xml-rpc https://stackoverflow.com/questions/40647779/how-to-call-an-api-which-is-based-on-xml-rpc-specification-in-c https://tldp.org/HOWTO/XML-RPC-HOWTO/xmlrpc-howto-php.html https://github.com/marcosbozzani/xmlrpcnet/tree/master/samples/SumAndDiffVB Step 1 : Created a Console Application in .NET Step 2 : Install the NuGet "xml-rpc.net" Step 3: Created a sample request model class like this, Step 4 : Created a sample response model class like this, Step 5 : Create an interface which is inherited form IXmlRpcProxy base class with the help of the namespace using CookComputing.XmlRpc; and this interface must contain our endpoint method and it should decorate with the filter XmlRpcUrl having the API resource. Step 6 : To make calls to an XML-RPC server it is necessary to use an instance of a proxy class. */ namespace CS_xml_rpc_client { public class response//Step 4 ~ xml-rpc { public int sum { get; set; } public int difference { get; set; } } [XmlRpcUrl("http://127.0.0.1:8080/phpxmlrpc/xmlrpc_server.php")]//Step 5 ~ xml-rpc public interface sumAndDifference : IXmlRpcProxy { [XmlRpcMethod("sample.sumAndDifference")]//endpoint name response sumAndDifference(int x,int y); } class Program { static void Pause() { Console.Write("Press any key to continue..."); Console.ReadKey(true); } static void Main(string[] args) { //--- //Step 6 ~ xml-rpc response response = new response(); sumAndDifference fun = XmlRpcProxyGen.Create(); response = fun.sumAndDifference(5,3); Console.WriteLine("response = {0},{1}", response.sum, response.difference); //---Step 6 ~ xml-rpc Pause(); } } }