///<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"); } }
/// <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); 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); } }