/// <summary>The local method dispatcher - do not invoke.</summary> override public IMessage Invoke(IMessage msg) { IMethodCallMessage methodMessage = (IMethodCallMessage)msg; _client.MethodName = _remoteObjectName + "." + methodMessage.MethodName; _client.Params.Clear(); foreach (Object o in methodMessage.Args) { _client.Params.Add(o); } try { Object ret = _client.Invoke(_url); return(new ReturnMessage(ret, null, 0, methodMessage.LogicalCallContext, methodMessage)); } catch (Exception e) { return(new ReturnMessage(e, methodMessage)); } }
public void HttpPost(SimpleHttpRequest req) { XmlRpcRequest rpc = XmlRpcRequestDeserializer.Parse(req.Input); XmlRpcResponse resp = new XmlRpcResponse(); Object target = _handlers[rpc.MethodNameObject]; if (target == null) { resp.SetFault(-1, "Object " + rpc.MethodNameObject + " not registered."); } else { try { resp.Value = rpc.Invoke(target); } catch (XmlRpcException e) { resp.SetFault(e.Code, e.Message); } catch (Exception e2) { resp.SetFault(-1, e2.Message); } } Logger.WriteEntry(resp.ToString(), EventLogEntryType.Information); SendHeader(req.Protocol, "text/xml", 0, " 200 OK", req.Output); req.Output.Flush(); XmlTextWriter xml = new XmlTextWriter(req.Output); XmlRpcResponseSerializer.Serialize(xml, resp); xml.Flush(); req.Output.Flush(); }
/// <summary>Submit two XML-RPC calls to Google. One to suggest spelling on a phrase and /// the next to do a basic search.</summary> public static void Main() { // Use the console logger above. Logger.Delegate = new Logger.LoggerDelegate(WriteEntry); // Send the sample.Ping RPC using the Send method which gives you a little more control... XmlRpcRequest client = new XmlRpcRequest(); client.MethodName = SPELL_CHECK; client.Params.Add(PHRASE); client.Params.Add(KEY); Console.WriteLine("Looking up: " + PHRASE); try { Console.WriteLine("Request: " + client); XmlRpcResponse response = client.Send(URL, 10000); Console.WriteLine("Response: " + response); if (response.IsFault) { Console.WriteLine("Fault {0}: {1}", response.FaultCode, response.FaultString); } else { Console.WriteLine("Returned: " + response.Value); } } catch (XmlRpcException serverException) { Console.WriteLine("Fault {0}: {1}", serverException.FaultCode, serverException.FaultString); } catch (Exception e) { Console.WriteLine("Exception " + e); } client.MethodName = RESULTS; client.Params.Clear(); client.Params.Add("C# XML-RPC .Net"); client.Params.Add(0); client.Params.Add(5); client.Params.Add(""); client.Params.Add(""); client.Params.Add(false); client.Params.Add(""); client.Params.Add(""); client.Params.Add(KEY); try { Hashtable results = (Hashtable)client.Invoke(URL); foreach (Hashtable result in ((Hashtable)results["resultElements"]).Values) { Console.WriteLine("Title: {0}",result["title"]); Console.WriteLine("URL: {0}",result["URL"]); Console.WriteLine("Context: {0}",result["snippet"]); Console.WriteLine("------------------------------------------------------------"); } } catch (XmlRpcException serverException) { Console.WriteLine("Fault {0}: {1}", serverException.FaultCode, serverException.FaultString); } catch (Exception e) { Console.WriteLine("Exception " + e); } }
/// <summary>Main application method.</summary> /// <remarks> Simply sets up logging and then an <c>XmlRpcRequest</c> instance. Then /// Calls <c>sample.Ping</c>, <c>sample.Echo</c> and <c>sample.Broken</c> in sequence. /// The <c>XmlRpcResponse</c> from each call is then displayed. Faults are checked for. /// </remarks> public static void Main(String[] args) { if (args.Length == 1) { URL = args[0]; } Console.WriteLine("Server: " + URL); // Use the console logger above. Logger.Delegate = new Logger.LoggerDelegate(WriteEntry); // Send the sample.Ping RPC using the Send method which gives you a little more control... XmlRpcRequest client = new XmlRpcRequest(); client.MethodName = "sample.Ping"; try { Console.WriteLine("Request: " + client); XmlRpcResponse response = client.Send(URL, 10000); Console.WriteLine("Response: " + response); if (response.IsFault) { Console.WriteLine("Fault {0}: {1}", response.FaultCode, response.FaultString); } else { Console.WriteLine("Returned: " + response.Value); } } catch (Exception e) { Console.WriteLine("Exception " + e); } // Invoke the sample.Echo RPC - Invoke more closely parallels a method invocation client.MethodName = "sample.Echo"; client.Params.Clear(); client.Params.Add("Hello"); try { String echo = (String)client.Invoke(URL); Console.WriteLine("Returned: " + echo); } catch (XmlRpcException serverException) { Console.WriteLine("Fault {0}: {1}", serverException.FaultCode, serverException.FaultString); } catch (Exception e) { Console.WriteLine("Exception " + e); } // Invoke sample.Broken RPC - method that is not present on server. client.MethodName = "sample.Broken"; client.Params.Clear(); try { Object response = client.Invoke(URL); Console.WriteLine("Response: " + response); } catch (XmlRpcException serverException) { Console.WriteLine("Fault {0}: {1}", serverException.FaultCode, serverException.FaultString); } catch (Exception e) { Console.WriteLine("Exception " + e); } }
///<summary>Sends request to MyPW and returns a result code</summary> ///<remarks>The result code will be: /// <list type="bullet"> /// <item>-99999 = MAJOR ERROR - request couldn't be sent (networking problems?)</item> /// <item>-99 = Site Authentication Failure (siteid/authkey)</item> /// <item>-2 = Token disabled</item> /// <item>-1 = Token not found. </item> /// <item>0 = Sucess! Authenticated!</item> /// <item>others = see message</item> /// </list> ///</remarks> public string Authenticate() { // client is the actual object that sends the request XmlRpcRequest client = new XmlRpcRequest(); client.MethodName = "auth.auth"; // Set the XML RPC Method client.Params.Clear(); // Make sure we remove all client data client.Params.Add(this.request); // Now add our data // try / catch -- this actuall sends the request to MyPW try { this.results = (Hashtable)client.Invoke("https://services.mypw.com/RPC2"); } catch (XmlRpcException serverException) { Console.WriteLine("[MyPWAuth] ERROR {0}: {1}", serverException.FaultCode, serverException.FaultString); } catch (Exception ex) { Console.WriteLine("[MyPWAuth] ERROR: " + ex.Message + " @ " + ex.TargetSite); } // try / catch -- return the result code as a string. try { return (Convert.ToString(this.results["code"])); } catch (Exception ex) { Console.WriteLine("[MyPWAuth] ERROR: Response parse failure. No valid response. @ " + ex.TargetSite); return ("-99999");