aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/XmlRpcCS/XmlRpcRequest.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--XmlRpcCS/XmlRpcRequest.cs150
1 files changed, 150 insertions, 0 deletions
diff --git a/XmlRpcCS/XmlRpcRequest.cs b/XmlRpcCS/XmlRpcRequest.cs
new file mode 100644
index 0000000..18d2182
--- /dev/null
+++ b/XmlRpcCS/XmlRpcRequest.cs
@@ -0,0 +1,150 @@
1namespace 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
13 internal class AcceptAllCertificatePolicy : ICertificatePolicy
14 {
15 public AcceptAllCertificatePolicy()
16 {
17 }
18
19 public bool CheckValidationResult(ServicePoint sPoint,
20 System.Security.Cryptography.X509Certificates.X509Certificate cert,
21 WebRequest wRequest, int certProb)
22 {
23 // Always accept
24 return true;
25 }
26 }
27
28 /// <summary>Class supporting the request side of an XML-RPC transaction.</summary>
29 public class XmlRpcRequest
30 {
31 private String _methodName = null;
32 private Encoding _encoding = new ASCIIEncoding();
33 private XmlRpcRequestSerializer _serializer = new XmlRpcRequestSerializer();
34 private XmlRpcResponseDeserializer _deserializer = new XmlRpcResponseDeserializer();
35
36 /// <summary><c>ArrayList</c> containing the parameters.</summary>
37 protected IList _params = null;
38
39 /// <summary>Instantiate an <c>XmlRpcRequest</c></summary>
40 public XmlRpcRequest()
41 {
42 _params = new ArrayList();
43 }
44
45 /// <summary>Instantiate an <c>XmlRpcRequest</c> for a specified method and parameters.</summary>
46 /// <param name="methodName"><c>String</c> designating the <i>object.method</i> on the server the request
47 /// should be directed to.</param>
48 /// <param name="parameters"><c>ArrayList</c> of XML-RPC type parameters to invoke the request with.</param>
49 public XmlRpcRequest(String methodName, IList parameters)
50 {
51 MethodName = methodName;
52 _params = parameters;
53 }
54
55 /// <summary><c>ArrayList</c> conntaining the parameters for the request.</summary>
56 public virtual IList Params
57 {
58 get { return _params; }
59 }
60
61 /// <summary><c>String</c> conntaining the method name, both object and method, that the request will be sent to.</summary>
62 public virtual String MethodName
63 {
64 get { return _methodName; }
65 set { _methodName = value; }
66 }
67
68 /// <summary><c>String</c> object name portion of the method name.</summary>
69 public String MethodNameObject
70 {
71 get
72 {
73 int index = MethodName.IndexOf(".");
74
75 if (index == -1)
76 return MethodName;
77
78 return MethodName.Substring(0, index);
79 }
80 }
81
82 /// <summary><c>String</c> method name portion of the object.method name.</summary>
83 public String MethodNameMethod
84 {
85 get
86 {
87 int index = MethodName.IndexOf(".");
88
89 if (index == -1)
90 return MethodName;
91
92 return MethodName.Substring(index + 1, MethodName.Length - index - 1);
93 }
94 }
95
96 /// <summary>Invoke this request on the server.</summary>
97 /// <param name="url"><c>String</c> The url of the XML-RPC server.</param>
98 /// <returns><c>Object</c> The value returned from the method invocation on the server.</returns>
99 /// <exception cref="XmlRpcException">If an exception generated on the server side.</exception>
100 public Object Invoke(String url)
101 {
102 XmlRpcResponse res = Send(url, 10000);
103
104 if (res.IsFault)
105 throw new XmlRpcException(res.FaultCode, res.FaultString);
106
107 return res.Value;
108 }
109
110 /// <summary>Send the request to the server.</summary>
111 /// <param name="url"><c>String</c> The url of the XML-RPC server.</param>
112 /// <param name="timeout">Milliseconds before the connection times out.</param>
113 /// <returns><c>XmlRpcResponse</c> The response generated.</returns>
114 public XmlRpcResponse Send(String url, int timeout)
115 {
116 // Override SSL authentication mechanisms
117 ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
118
119 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
120 if (request == null)
121 throw new XmlRpcException(XmlRpcErrorCodes.TRANSPORT_ERROR,
122 XmlRpcErrorCodes.TRANSPORT_ERROR_MSG + ": Could not create request with " + url);
123 request.Method = "POST";
124 request.ContentType = "text/xml";
125 request.AllowWriteStreamBuffering = true;
126 request.Timeout = timeout;
127
128 Stream stream = request.GetRequestStream();
129 XmlTextWriter xml = new XmlTextWriter(stream, _encoding);
130 _serializer.Serialize(xml, this);
131 xml.Flush();
132 xml.Close();
133
134 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
135 StreamReader input = new StreamReader(response.GetResponseStream());
136
137 XmlRpcResponse resp = (XmlRpcResponse)_deserializer.Deserialize(input);
138 input.Close();
139 response.Close();
140 return resp;
141 }
142
143 /// <summary>Produce <c>String</c> representation of the object.</summary>
144 /// <returns><c>String</c> representation of the object.</returns>
145 override public String ToString()
146 {
147 return _serializer.Serialize(this);
148 }
149 }
150}