XmlRpcRequest.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (c) Contributors, http://www.openmetaverse.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. */
  28. namespace Nwc.XmlRpc
  29. {
  30. using System;
  31. using System.Collections;
  32. using System.IO;
  33. using System.Xml;
  34. using System.Net;
  35. using System.Text;
  36. using System.Reflection;
  37. using System.Net.Security;
  38. using System.Security.Cryptography.X509Certificates;
  39. internal class AcceptAllCertificatePolicy : ICertificatePolicy
  40. {
  41. public AcceptAllCertificatePolicy()
  42. {
  43. }
  44. public bool CheckValidationResult(ServicePoint sPoint,
  45. System.Security.Cryptography.X509Certificates.X509Certificate cert,
  46. WebRequest wRequest, int certProb)
  47. {
  48. // Always accept
  49. return true;
  50. }
  51. }
  52. /// <summary>Class supporting the request side of an XML-RPC transaction.</summary>
  53. public class XmlRpcRequest
  54. {
  55. private String _methodName = null;
  56. private Encoding _encoding = new ASCIIEncoding();
  57. private XmlRpcRequestSerializer _serializer = new XmlRpcRequestSerializer();
  58. private XmlRpcResponseDeserializer _deserializer = new XmlRpcResponseDeserializer();
  59. /// <summary><c>ArrayList</c> containing the parameters.</summary>
  60. protected IList _params = null;
  61. /// <summary>Instantiate an <c>XmlRpcRequest</c></summary>
  62. public XmlRpcRequest()
  63. {
  64. _params = new ArrayList();
  65. }
  66. /// <summary>Instantiate an <c>XmlRpcRequest</c> for a specified method and parameters.</summary>
  67. /// <param name="methodName"><c>String</c> designating the <i>object.method</i> on the server the request
  68. /// should be directed to.</param>
  69. /// <param name="parameters"><c>ArrayList</c> of XML-RPC type parameters to invoke the request with.</param>
  70. public XmlRpcRequest(String methodName, IList parameters)
  71. {
  72. MethodName = methodName;
  73. _params = parameters;
  74. }
  75. /// <summary><c>ArrayList</c> conntaining the parameters for the request.</summary>
  76. public virtual IList Params
  77. {
  78. get { return _params; }
  79. }
  80. /// <summary><c>String</c> conntaining the method name, both object and method, that the request will be sent to.</summary>
  81. public virtual String MethodName
  82. {
  83. get { return _methodName; }
  84. set { _methodName = value; }
  85. }
  86. /// <summary><c>String</c> object name portion of the method name.</summary>
  87. public String MethodNameObject
  88. {
  89. get
  90. {
  91. int index = MethodName.IndexOf(".");
  92. if (index == -1)
  93. return MethodName;
  94. return MethodName.Substring(0, index);
  95. }
  96. }
  97. /// <summary><c>String</c> method name portion of the object.method name.</summary>
  98. public String MethodNameMethod
  99. {
  100. get
  101. {
  102. int index = MethodName.IndexOf(".");
  103. if (index == -1)
  104. return MethodName;
  105. return MethodName.Substring(index + 1, MethodName.Length - index - 1);
  106. }
  107. }
  108. /// <summary>Invoke this request on the server.</summary>
  109. /// <param name="url"><c>String</c> The url of the XML-RPC server.</param>
  110. /// <returns><c>Object</c> The value returned from the method invocation on the server.</returns>
  111. /// <exception cref="XmlRpcException">If an exception generated on the server side.</exception>
  112. public Object Invoke(String url)
  113. {
  114. XmlRpcResponse res = Send(url, 10000);
  115. if (res.IsFault)
  116. throw new XmlRpcException(res.FaultCode, res.FaultString);
  117. return res.Value;
  118. }
  119. /// <summary>Send the request to the server.</summary>
  120. /// <param name="url"><c>String</c> The url of the XML-RPC server.</param>
  121. /// <param name="timeout">Milliseconds before the connection times out.</param>
  122. /// <returns><c>XmlRpcResponse</c> The response generated.</returns>
  123. public XmlRpcResponse Send(String url, int timeout)
  124. {
  125. // Override SSL authentication mechanisms
  126. ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
  127. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  128. if (request == null)
  129. throw new XmlRpcException(XmlRpcErrorCodes.TRANSPORT_ERROR,
  130. XmlRpcErrorCodes.TRANSPORT_ERROR_MSG + ": Could not create request with " + url);
  131. request.Method = "POST";
  132. request.ContentType = "text/xml";
  133. request.AllowWriteStreamBuffering = true;
  134. request.Timeout = timeout;
  135. Stream stream = request.GetRequestStream();
  136. XmlTextWriter xml = new XmlTextWriter(stream, _encoding);
  137. _serializer.Serialize(xml, this);
  138. xml.Flush();
  139. xml.Close();
  140. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  141. StreamReader input = new StreamReader(response.GetResponseStream());
  142. XmlRpcResponse resp = (XmlRpcResponse)_deserializer.Deserialize(input);
  143. input.Close();
  144. response.Close();
  145. return resp;
  146. }
  147. /// <summary>Produce <c>String</c> representation of the object.</summary>
  148. /// <returns><c>String</c> representation of the object.</returns>
  149. override public String ToString()
  150. {
  151. return _serializer.Serialize(this);
  152. }
  153. }
  154. }