XmlRpcRequest.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. namespace Nwc.XmlRpc
  2. {
  3. using System;
  4. using System.Collections;
  5. using System.IO;
  6. using System.Xml;
  7. using System.Net;
  8. using System.Text;
  9. using System.Reflection;
  10. using System.Net.Security;
  11. using System.Security.Cryptography.X509Certificates;
  12. internal class AcceptAllCertificatePolicy : ICertificatePolicy
  13. {
  14. public AcceptAllCertificatePolicy()
  15. {
  16. }
  17. public bool CheckValidationResult(ServicePoint sPoint,
  18. System.Security.Cryptography.X509Certificates.X509Certificate cert,
  19. WebRequest wRequest, int certProb)
  20. {
  21. // Always accept
  22. return true;
  23. }
  24. }
  25. /// <summary>Class supporting the request side of an XML-RPC transaction.</summary>
  26. public class XmlRpcRequest
  27. {
  28. private String _methodName = null;
  29. private Encoding _encoding = new ASCIIEncoding();
  30. private XmlRpcRequestSerializer _serializer = new XmlRpcRequestSerializer();
  31. private XmlRpcResponseDeserializer _deserializer = new XmlRpcResponseDeserializer();
  32. /// <summary><c>ArrayList</c> containing the parameters.</summary>
  33. protected IList _params = null;
  34. /// <summary>Instantiate an <c>XmlRpcRequest</c></summary>
  35. public XmlRpcRequest()
  36. {
  37. _params = new ArrayList();
  38. }
  39. /// <summary>Instantiate an <c>XmlRpcRequest</c> for a specified method and parameters.</summary>
  40. /// <param name="methodName"><c>String</c> designating the <i>object.method</i> on the server the request
  41. /// should be directed to.</param>
  42. /// <param name="parameters"><c>ArrayList</c> of XML-RPC type parameters to invoke the request with.</param>
  43. public XmlRpcRequest(String methodName, IList parameters)
  44. {
  45. MethodName = methodName;
  46. _params = parameters;
  47. }
  48. /// <summary><c>ArrayList</c> conntaining the parameters for the request.</summary>
  49. public virtual IList Params
  50. {
  51. get { return _params; }
  52. }
  53. /// <summary><c>String</c> conntaining the method name, both object and method, that the request will be sent to.</summary>
  54. public virtual String MethodName
  55. {
  56. get { return _methodName; }
  57. set { _methodName = value; }
  58. }
  59. /// <summary><c>String</c> object name portion of the method name.</summary>
  60. public String MethodNameObject
  61. {
  62. get
  63. {
  64. int index = MethodName.IndexOf(".");
  65. if (index == -1)
  66. return MethodName;
  67. return MethodName.Substring(0, index);
  68. }
  69. }
  70. /// <summary><c>String</c> method name portion of the object.method name.</summary>
  71. public String MethodNameMethod
  72. {
  73. get
  74. {
  75. int index = MethodName.IndexOf(".");
  76. if (index == -1)
  77. return MethodName;
  78. return MethodName.Substring(index + 1, MethodName.Length - index - 1);
  79. }
  80. }
  81. /// <summary>Invoke this request on the server.</summary>
  82. /// <param name="url"><c>String</c> The url of the XML-RPC server.</param>
  83. /// <returns><c>Object</c> The value returned from the method invocation on the server.</returns>
  84. /// <exception cref="XmlRpcException">If an exception generated on the server side.</exception>
  85. public Object Invoke(String url)
  86. {
  87. XmlRpcResponse res = Send(url, 10000);
  88. if (res.IsFault)
  89. throw new XmlRpcException(res.FaultCode, res.FaultString);
  90. return res.Value;
  91. }
  92. /// <summary>Send the request to the server.</summary>
  93. /// <param name="url"><c>String</c> The url of the XML-RPC server.</param>
  94. /// <param name="timeout">Milliseconds before the connection times out.</param>
  95. /// <returns><c>XmlRpcResponse</c> The response generated.</returns>
  96. public XmlRpcResponse Send(String url, int timeout)
  97. {
  98. // Override SSL authentication mechanisms
  99. ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
  100. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  101. if (request == null)
  102. throw new XmlRpcException(XmlRpcErrorCodes.TRANSPORT_ERROR,
  103. XmlRpcErrorCodes.TRANSPORT_ERROR_MSG + ": Could not create request with " + url);
  104. request.Method = "POST";
  105. request.ContentType = "text/xml";
  106. request.AllowWriteStreamBuffering = true;
  107. request.Timeout = timeout;
  108. Stream stream = request.GetRequestStream();
  109. XmlTextWriter xml = new XmlTextWriter(stream, _encoding);
  110. _serializer.Serialize(xml, this);
  111. xml.Flush();
  112. xml.Close();
  113. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  114. StreamReader input = new StreamReader(response.GetResponseStream());
  115. XmlRpcResponse resp = (XmlRpcResponse)_deserializer.Deserialize(input);
  116. input.Close();
  117. response.Close();
  118. return resp;
  119. }
  120. /// <summary>Produce <c>String</c> representation of the object.</summary>
  121. /// <returns><c>String</c> representation of the object.</returns>
  122. override public String ToString()
  123. {
  124. return _serializer.Serialize(this);
  125. }
  126. }
  127. }