/// <summary> /// Sends the specified message to an XML-RPC server to execute a remote procedure call. /// </summary> /// <param name="message">A <see cref="XmlRpcMessage"/> that represents the information needed to execute the remote procedure call.</param> /// <returns>A <see cref="XmlRpcResponse"/> that represents the server's response to the remote procedure call.</returns> /// <exception cref="ArgumentNullException">The <paramref name="message"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="InvalidOperationException">The <see cref="Host"/> is a <b>null</b> reference (Nothing in Visual Basic).</exception> /// <exception cref="InvalidOperationException">This <see cref="XmlRpcClient"/> has a <see cref="SendAsync(XmlRpcMessage, Object)"/> call in progress.</exception> public XmlRpcResponse Send(XmlRpcMessage message) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ XmlRpcResponse response = null; //------------------------------------------------------------ // Validate parameter //------------------------------------------------------------ Guard.ArgumentNotNull(message, "message"); //------------------------------------------------------------ // Validate client state //------------------------------------------------------------ if(this.Host == null) { throw new InvalidOperationException(String.Format(null, "Unable to send XML-RPC message. The Host property has not been initialized. \n\r Message payload: {0}", message)); } else if (this.SendOperationInProgress) { throw new InvalidOperationException(String.Format(null, "Unable to send XML-RPC message. The XmlRpcClient has a SendAsync call in progress. \n\r Message payload: {0}", message)); } //------------------------------------------------------------ // Execute the remote procedure call //------------------------------------------------------------ WebRequest webRequest = XmlRpcClient.CreateWebRequest(this.Host, this.UserAgent, message, this.UseDefaultCredentials, this.clientOptions); using (WebResponse webResponse = (WebResponse)webRequest.GetResponse()) { response = new XmlRpcResponse(webResponse); } return response; }
/// <summary> /// Called when a corresponding asynchronous send operation completes. /// </summary> /// <param name="result">The result of the asynchronous operation.</param> private static void AsyncSendCallback(IAsyncResult result) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ XmlRpcResponse response = null; WebRequest httpWebRequest = null; XmlRpcClient client = null; Uri host = null; XmlRpcMessage message = null; WebRequestOptions options = null; object userToken = null; //------------------------------------------------------------ // Determine if the async send operation completed //------------------------------------------------------------ if (result.IsCompleted) { //------------------------------------------------------------ // Extract the send operations parameters from the user state //------------------------------------------------------------ object[] parameters = (object[])result.AsyncState; httpWebRequest = parameters[0] as WebRequest; client = parameters[1] as XmlRpcClient; host = parameters[2] as Uri; message = parameters[3] as XmlRpcMessage; options = parameters[4] as WebRequestOptions; userToken = parameters[5]; //------------------------------------------------------------ // Verify expected parameters were found //------------------------------------------------------------ if (client != null) { //------------------------------------------------------------ // Get the HTTP response to the remote procedure call //------------------------------------------------------------ WebResponse httpWebResponse = (WebResponse)httpWebRequest.EndGetResponse(result); //------------------------------------------------------------ // Extract the XML-RPC response to the remote procedure call //------------------------------------------------------------ response = new XmlRpcResponse(httpWebResponse); //------------------------------------------------------------ // Raise SendCompleted event to notify registered handlers of state change //------------------------------------------------------------ client.OnMessageSent(new XmlRpcMessageSentEventArgs(host, message, response, options, userToken)); //------------------------------------------------------------ // Reset async operation in progress indicator //------------------------------------------------------------ client.SendOperationInProgress = false; } } }
/// <summary> /// Initializes a new instance of the <see cref="XmlRpcMessageSentEventArgs"/> class using the supplied parameters. /// </summary> /// <param name="host">A <see cref="Uri"/> that represents the URL of the host computer used for the XML-RPC transaction.</param> /// <param name="message">An <see cref="XmlRpcMessage"/> that represents the remote procedure call payload.</param> /// <param name="response">An <see cref="XmlRpcResponse"/> that represents the response to the remote procedure call.</param> /// <param name="credentials">A <see cref="ICredentials"/> that represents the authentication credentials utilized by the client when making the remote procedure call. This parameter may be <b>null</b>.</param> /// <param name="proxy">A <see cref="IWebProxy"/> that represents the web proxy utilized by the client to proxy the remote procedure call. This parameter may be <b>null</b>.</param> /// <param name="state">A <see cref="Object"/> containing state information that was passed to the asynchronous send operation. This parameter may be <b>null</b>.</param> /// <exception cref="ArgumentNullException">The <paramref name="host"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentNullException">The <paramref name="message"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentNullException">The <paramref name="response"/> is a null reference (Nothing in Visual Basic).</exception> public XmlRpcMessageSentEventArgs(Uri host, XmlRpcMessage message, XmlRpcResponse response, ICredentials credentials, IWebProxy proxy, Object state) { //------------------------------------------------------------ // Validate parameters //------------------------------------------------------------ Guard.ArgumentNotNull(host, "host"); Guard.ArgumentNotNull(message, "message"); Guard.ArgumentNotNull(response, "response"); //------------------------------------------------------------ // Initialize class members //------------------------------------------------------------ eventHost = host; eventMessage = message; eventResponse = response; eventOptions = new WebRequestOptions(credentials, proxy); eventUserToken = state; }