コード例 #1
0
        //============================================================
        //	ICOMPARABLE IMPLEMENTATION
        //============================================================
        #region CompareTo(object obj)
        /// <summary>
        /// Compares the current instance with another object of the same type.
        /// </summary>
        /// <param name="obj">An object to compare with this instance.</param>
        /// <returns>A 32-bit signed integer that indicates the relative order of the objects being compared.</returns>
        /// <exception cref="ArgumentException">The <paramref name="obj"/> is not the expected <see cref="Type"/>.</exception>
        public int CompareTo(object obj)
        {
            //------------------------------------------------------------
            //	If target is a null reference, instance is greater
            //------------------------------------------------------------
            if (obj == null)
            {
                return(1);
            }

            //------------------------------------------------------------
            //	Determine comparison result using property state of objects
            //------------------------------------------------------------
            TrackbackMessage value = obj as TrackbackMessage;

            if (value != null)
            {
                int result = String.Compare(this.Encoding.WebName, value.Encoding.WebName, StringComparison.OrdinalIgnoreCase);
                result = result | String.Compare(this.Excerpt, value.Excerpt, StringComparison.OrdinalIgnoreCase);
                result = result | Uri.Compare(this.Permalink, value.Permalink, UriComponents.AbsoluteUri, UriFormat.SafeUnescaped, StringComparison.OrdinalIgnoreCase);
                result = result | String.Compare(this.Title, value.Title, StringComparison.OrdinalIgnoreCase);
                result = result | String.Compare(this.WeblogName, value.WeblogName, StringComparison.OrdinalIgnoreCase);

                return(result);
            }
            else
            {
                throw new ArgumentException(String.Format(null, "obj is not of type {0}, type was found to be '{1}'.", this.GetType().FullName, obj.GetType().FullName), "obj");
            }
        }
コード例 #2
0
        //============================================================
        //    CLASS SUMMARY
        //============================================================
        /// <summary>
        /// Provides example code for the TrackbackClient class.
        /// </summary>
        public static void ClassExample()
        {
            #region TrackbackClient
            // Initialize the Trackback peer-to-peer notification protocol client
            TrackbackClient client      = new TrackbackClient();
            client.Host                 = new Uri("http://www.example.com/trackback/5");

            // Construct the trackback message to be sent
            TrackbackMessage message    = new TrackbackMessage(new Uri("http://www.bar.com/"));
            message.Encoding            = Encoding.UTF8;
            message.WeblogName          = "Foo";
            message.Title               = "Foo Bar";
            message.Excerpt             = "My Excerpt";

            // Send a synchronous trackback ping
            TrackbackResponse response  = client.Send(message);

            // Verify response to the trackback ping
            if (response != null)
            {
                if (response.HasError)
                {
                    // Use the TrackbackResponse.ErrorMessage property to determine the reason the trackback ping failed
                }
            }
            #endregion
        }
コード例 #3
0
        /// <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)
        {
            TrackbackResponse response       = null;
            WebRequest        httpWebRequest = null;
            TrackbackClient   client         = null;
            Uri host = null;
            TrackbackMessage  message   = null;
            WebRequestOptions options   = null;
            object            userToken = null;

            if (result.IsCompleted)
            {
                object[] parameters = (object[])result.AsyncState;
                httpWebRequest = parameters[0] as WebRequest;
                client         = parameters[1] as TrackbackClient;
                host           = parameters[2] as Uri;
                message        = parameters[3] as TrackbackMessage;
                options        = parameters[4] as WebRequestOptions;
                userToken      = parameters[5];

                if (client != null)
                {
                    WebResponse httpWebResponse = (WebResponse)httpWebRequest.EndGetResponse(result);

                    response = new TrackbackResponse(httpWebResponse);

                    client.OnMessageSent(new TrackbackMessageSentEventArgs(host, message, response, options, userToken));

                    client.SendOperationInProgress = false;
                }
            }
        }
コード例 #4
0
        /// <summary>
        /// Sends the specified message to an Trackback server to execute an Trackback ping request.
        /// This method does not block the calling thread and allows the caller to pass an object to the method that is invoked when the operation completes.
        /// </summary>
        /// <param name="message">A <see cref="TrackbackMessage"/> that represents the information needed to execute the Trackback ping request.</param>
        /// <param name="userToken">A user-defined object that is passed to the method invoked when the asynchronous operation completes.</param>
        /// <remarks>
        ///     <para>
        ///         To receive notification when the Trackback ping request has been sent or the operation has been cancelled, add an event handler to the <see cref="SendCompleted"/> event.
        ///         You can cancel a <see cref="SendAsync(TrackbackMessage, Object)"/> operation by calling the <see cref="SendAsyncCancel()"/> method.
        ///     </para>
        /// </remarks>
        /// <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="TrackbackClient"/> has a <see cref="SendAsync(TrackbackMessage, Object)"/> call in progress.</exception>
        //[HostProtectionAttribute(SecurityAction.LinkDemand, ExternalThreading = true)]
        public void SendAsync(TrackbackMessage message, Object userToken)
        {
            Guard.ArgumentNotNull(message, "message");

            if (this.Host == null)
            {
                throw new InvalidOperationException(String.Format(null, "Unable to send Trackback 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 Trackback message. The TrackbackClient has a SendAsync call in progress. \n\r Message payload: {0}", message));
            }

            this.SendOperationInProgress   = true;
            this.AsyncSendHasBeenCancelled = false;

            asyncHttpWebRequest = TrackbackClient.CreateWebRequest(this.Host, this.UserAgent, message, this.UseDefaultCredentials, this.clientOptions);

            object[] state = new object[6] {
                asyncHttpWebRequest, this, this.Host, message, this.clientOptions, userToken
            };
            IAsyncResult result = asyncHttpWebRequest.BeginGetResponse(new AsyncCallback(AsyncSendCallback), state);

            ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(AsyncTimeoutCallback), state, this.Timeout, true);
        }
コード例 #5
0
        //============================================================
        //	PRIVATE METHODS
        //============================================================
        #region CreateWebRequest(Uri host, string userAgent, TrackbackMessage message, bool useDefaultCredentials, WebRequestOptions options)
        /// <summary>
        /// Initializes a new <see cref="WebRequest"/> suitable for sending an Trackback ping request using the supplied host, user agent, message, credentials, and proxy.
        /// </summary>
        /// <param name="host">A <see cref="Uri"/> that represents the URL of the host computer used for Trackback transactions.</param>
        /// <param name="userAgent">Information such as the application name, version, host operating system, and language.</param>
        /// <param name="message">A <see cref="TrackbackMessage"/> that represents the information needed to execute the Trackback ping request.</param>
        /// <param name="useDefaultCredentials">
        ///     Controls whether the <see cref="CredentialCache.DefaultCredentials">DefaultCredentials</see> are sent when making Trackback pings.
        /// </param>
        /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="host"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="userAgent"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="userAgent"/> is an empty string.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="message"/> is a null reference (Nothing in Visual Basic).</exception>
        private static WebRequest CreateWebRequest(Uri host, string userAgent, TrackbackMessage message, bool useDefaultCredentials, WebRequestOptions options)
        {
            //------------------------------------------------------------
            //	Local members
            //------------------------------------------------------------
            HttpWebRequest httpRequest = null;

            byte[] payloadData;

            //------------------------------------------------------------
            //	Validate parameters
            //------------------------------------------------------------
            Guard.ArgumentNotNull(host, "host");
            Guard.ArgumentNotNullOrEmptyString(userAgent, "userAgent");
            Guard.ArgumentNotNull(message, "message");

            //------------------------------------------------------------
            //	Build the Trackback payload data
            //------------------------------------------------------------
            using (MemoryStream stream = new MemoryStream())
            {
                using (StreamWriter writer = new StreamWriter(stream, message.Encoding))
                {
                    message.WriteTo(writer);
                    writer.Flush();

                    stream.Seek(0, SeekOrigin.Begin);
                    payloadData = message.Encoding.GetBytes((new StreamReader(stream)).ReadToEnd());
                }
            }

            //------------------------------------------------------------
            //	Build Trackback web request used to send an Trackback ping request
            //------------------------------------------------------------
            httpRequest               = (HttpWebRequest)HttpWebRequest.Create(host);
            httpRequest.Method        = "POST";
            httpRequest.ContentLength = payloadData.Length;
            httpRequest.ContentType   = String.Format(null, "application/x-www-form-urlencoded; charset={0}", message.Encoding.WebName);
            httpRequest.UserAgent     = userAgent;
            if (options != null)
            {
                options.ApplyOptions(httpRequest);
            }

            if (useDefaultCredentials)
            {
                httpRequest.Credentials = CredentialCache.DefaultCredentials;
            }

            using (Stream stream = httpRequest.GetRequestStream())
            {
                stream.Write(payloadData, 0, payloadData.Length);
            }

            return(httpRequest);
        }
コード例 #6
0
        //============================================================
        //	CALLBACK DELEGATE METHODS
        //============================================================
        #region AsyncSendCallback(IAsyncResult result)
        /// <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
            //------------------------------------------------------------
            TrackbackResponse response       = null;
            WebRequest        httpWebRequest = null;
            TrackbackClient   client         = null;
            Uri host = null;
            TrackbackMessage  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 TrackbackClient;
                host           = parameters[2] as Uri;
                message        = parameters[3] as TrackbackMessage;
                options        = parameters[4] as WebRequestOptions;
                userToken      = parameters[5];

                //------------------------------------------------------------
                //	Verify expected parameters were found
                //------------------------------------------------------------
                if (client != null)
                {
                    //------------------------------------------------------------
                    //	Get the Trackback response to the Trackback ping request
                    //------------------------------------------------------------
                    WebResponse httpWebResponse = (WebResponse)httpWebRequest.EndGetResponse(result);

                    //------------------------------------------------------------
                    //	Extract the Trackback response to the Trackback ping request
                    //------------------------------------------------------------
                    response = new TrackbackResponse(httpWebResponse);

                    //------------------------------------------------------------
                    //	Raise SendCompleted event to notify registered handlers of state change
                    //------------------------------------------------------------
                    client.OnMessageSent(new TrackbackMessageSentEventArgs(host, message, response, options, userToken));

                    //------------------------------------------------------------
                    //	Reset async operation in progress indicator
                    //------------------------------------------------------------
                    client.SendOperationInProgress = false;
                }
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="TrackbackMessageSentEventArgs"/> 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="TrackbackMessage"/> that represents the Trackback ping request payload.</param>
        /// <param name="response">An <see cref="TrackbackResponse"/> that represents the response to the Trackback ping request.</param>
        /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</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 TrackbackMessageSentEventArgs(Uri host, TrackbackMessage message, TrackbackResponse response, WebRequestOptions options, Object state)
        {
            Guard.ArgumentNotNull(host, "host");
            Guard.ArgumentNotNull(message, "message");
            Guard.ArgumentNotNull(response, "response");

            eventHost      = host;
            eventMessage   = message;
            eventResponse  = response;
            eventOptions   = options ?? new WebRequestOptions();
            eventUserToken = state;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="TrackbackMessageSentEventArgs"/> 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="TrackbackMessage"/> that represents the Trackback ping request payload.</param>
        /// <param name="response">An <see cref="TrackbackResponse"/> that represents the response to the Trackback ping request.</param>
        /// <param name="credentials">A <see cref="ICredentials"/> that represents the authentication credentials utilized by the client when making the Trackback ping request. 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 Trackback ping request. 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 TrackbackMessageSentEventArgs(Uri host, TrackbackMessage message, TrackbackResponse response, ICredentials credentials, IWebProxy proxy, Object state)
        {
            Guard.ArgumentNotNull(host, "host");
            Guard.ArgumentNotNull(message, "message");
            Guard.ArgumentNotNull(response, "response");

            eventHost      = host;
            eventMessage   = message;
            eventResponse  = response;
            eventOptions   = new WebRequestOptions(credentials, proxy);
            eventUserToken = state;
        }
コード例 #9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TrackbackMessageSentEventArgs"/> 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="TrackbackMessage"/> that represents the Trackback ping request payload.</param>
        /// <param name="response">An <see cref="TrackbackResponse"/> that represents the response to the Trackback ping request.</param>
        /// <param name="credentials">A <see cref="ICredentials"/> that represents the authentication credentials utilized by the client when making the Trackback ping request. 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 Trackback ping request. 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 TrackbackMessageSentEventArgs(Uri host, TrackbackMessage message, TrackbackResponse 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;
        }
コード例 #10
0
        public void SendAsync(TrackbackMessage message, Object userToken)
        {
            //------------------------------------------------------------
            //	Validate parameter
            //------------------------------------------------------------
            Guard.ArgumentNotNull(message, "message");

            //------------------------------------------------------------
            //	Validate client state
            //------------------------------------------------------------
            if (this.Host == null)
            {
                throw new InvalidOperationException(String.Format(null, "Unable to send Trackback 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 Trackback message. The TrackbackClient has a SendAsync call in progress. \n\r Message payload: {0}", message));
            }

            //------------------------------------------------------------
            //	Set async operation tracking indicators
            //------------------------------------------------------------
            this.SendOperationInProgress   = true;
            this.AsyncSendHasBeenCancelled = false;

            //------------------------------------------------------------
            //	Build Trackback web request used to send the Trackback ping request
            //------------------------------------------------------------
            asyncHttpWebRequest = TrackbackClient.CreateWebRequest(this.Host, this.UserAgent, message, this.UseDefaultCredentials, this.clientOptions);

            //------------------------------------------------------------
            //	Get the async response to the web request
            //------------------------------------------------------------
            object[] state = new object[6] {
                asyncHttpWebRequest, this, this.Host, message, this.clientOptions, userToken
            };
            IAsyncResult result = asyncHttpWebRequest.BeginGetResponse(new AsyncCallback(AsyncSendCallback), state);

            //------------------------------------------------------------
            //  Register the timeout callback
            //------------------------------------------------------------
            ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(AsyncTimeoutCallback), state, this.Timeout, true);
        }
コード例 #11
0
        //============================================================
        //	PUBLIC METHODS
        //============================================================
        #region Send(TrackbackMessage message)
        /// <summary>
        /// Sends the specified message to a Trackback server to execute an Trackback ping request.
        /// </summary>
        /// <param name="message">A <see cref="TrackbackMessage"/> that represents the information needed to execute the Trackback ping request.</param>
        /// <returns>A <see cref="TrackbackResponse"/> that represents the server's response to the Trackback ping request.</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="TrackbackClient"/> has a <see cref="SendAsync(TrackbackMessage, Object)"/> call in progress.</exception>
        public TrackbackResponse Send(TrackbackMessage message)
        {
            //------------------------------------------------------------
            //	Local members
            //------------------------------------------------------------
            TrackbackResponse response = null;

            //------------------------------------------------------------
            //	Validate parameter
            //------------------------------------------------------------
            Guard.ArgumentNotNull(message, "message");

            //------------------------------------------------------------
            //	Validate client state
            //------------------------------------------------------------
            if (this.Host == null)
            {
                throw new InvalidOperationException(String.Format(null, "Unable to send Trackback 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 Trackback message. The TrackbackClient has a SendAsync call in progress. \n\r Message payload: {0}", message));
            }

            //------------------------------------------------------------
            //	Execute the Trackback ping request
            //------------------------------------------------------------
            WebRequest webRequest = TrackbackClient.CreateWebRequest(this.Host, this.UserAgent, message, this.UseDefaultCredentials, this.clientOptions);

            using (WebResponse webResponse = (WebResponse)webRequest.GetResponse())
            {
                response = new TrackbackResponse(webResponse);
            }

            return(response);
        }
コード例 #12
0
        /// <summary>
        /// Compares the current instance with another object of the same type.
        /// </summary>
        /// <param name="obj">An object to compare with this instance.</param>
        /// <returns>A 32-bit signed integer that indicates the relative order of the objects being compared.</returns>
        /// <exception cref="ArgumentException">The <paramref name="obj"/> is not the expected <see cref="Type"/>.</exception>
        public int CompareTo(object obj)
        {
            if (obj == null)
            {
                return(1);
            }

            TrackbackMessage value = obj as TrackbackMessage;

            if (value != null)
            {
                int result = String.Compare(this.Encoding.WebName, value.Encoding.WebName, StringComparison.OrdinalIgnoreCase);
                result = result | String.Compare(this.Excerpt, value.Excerpt, StringComparison.OrdinalIgnoreCase);
                result = result | Uri.Compare(this.Permalink, value.Permalink, UriComponents.AbsoluteUri, UriFormat.SafeUnescaped, StringComparison.OrdinalIgnoreCase);
                result = result | String.Compare(this.Title, value.Title, StringComparison.OrdinalIgnoreCase);
                result = result | String.Compare(this.WeblogName, value.WeblogName, StringComparison.OrdinalIgnoreCase);

                return(result);
            }
            else
            {
                throw new ArgumentException(String.Format(null, "obj is not of type {0}, type was found to be '{1}'.", this.GetType().FullName, obj.GetType().FullName), "obj");
            }
        }
コード例 #13
0
ファイル: TrackbackClient.cs プロジェクト: Jiyuu/Argotic
        /// <summary>
        /// Initializes a new <see cref="WebRequest"/> suitable for sending an Trackback ping request using the supplied host, user agent, message, credentials, and proxy.
        /// </summary>
        /// <param name="host">A <see cref="Uri"/> that represents the URL of the host computer used for Trackback transactions.</param>
        /// <param name="userAgent">Information such as the application name, version, host operating system, and language.</param>
        /// <param name="message">A <see cref="TrackbackMessage"/> that represents the information needed to execute the Trackback ping request.</param>
        /// <param name="useDefaultCredentials">
        ///     Controls whether the <see cref="CredentialCache.DefaultCredentials">DefaultCredentials</see> are sent when making Trackback pings.
        /// </param>
        /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="host"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="userAgent"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="userAgent"/> is an empty string.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="message"/> is a null reference (Nothing in Visual Basic).</exception>
        private static WebRequest CreateWebRequest(Uri host, string userAgent, TrackbackMessage message, bool useDefaultCredentials, WebRequestOptions options)
        {
            //------------------------------------------------------------
            //	Local members
            //------------------------------------------------------------
            HttpWebRequest httpRequest  = null;
            byte[] payloadData;

            //------------------------------------------------------------
            //	Validate parameters
            //------------------------------------------------------------
            Guard.ArgumentNotNull(host, "host");
            Guard.ArgumentNotNullOrEmptyString(userAgent, "userAgent");
            Guard.ArgumentNotNull(message, "message");

            //------------------------------------------------------------
            //	Build the Trackback payload data
            //------------------------------------------------------------
            using(MemoryStream stream = new MemoryStream())
            {
                using(StreamWriter writer = new StreamWriter(stream, message.Encoding))
                {
                    message.WriteTo(writer);
                    writer.Flush();

                    stream.Seek(0, SeekOrigin.Begin);
                    payloadData = message.Encoding.GetBytes((new StreamReader(stream)).ReadToEnd());
                }
            }

            //------------------------------------------------------------
            //	Build Trackback web request used to send an Trackback ping request
            //------------------------------------------------------------
            httpRequest                     = (HttpWebRequest)HttpWebRequest.Create(host);
            httpRequest.Method              = "POST";
            httpRequest.ContentLength       = payloadData.Length;
            httpRequest.ContentType         = String.Format(null, "application/x-www-form-urlencoded; charset={0}", message.Encoding.WebName);
            httpRequest.UserAgent           = userAgent;
            if (options != null) options.ApplyOptions(httpRequest);

            if(useDefaultCredentials)
            {
                httpRequest.Credentials     = CredentialCache.DefaultCredentials;
            }

            using (Stream stream = httpRequest.GetRequestStream())
            {
                stream.Write(payloadData, 0, payloadData.Length);
            }

            return httpRequest;
        }
コード例 #14
0
ファイル: TrackbackClient.cs プロジェクト: Jiyuu/Argotic
        public void SendAsync(TrackbackMessage message, Object userToken)
        {
            //------------------------------------------------------------
            //	Validate parameter
            //------------------------------------------------------------
            Guard.ArgumentNotNull(message, "message");

            //------------------------------------------------------------
            //	Validate client state
            //------------------------------------------------------------
            if (this.Host == null)
            {
                throw new InvalidOperationException(String.Format(null, "Unable to send Trackback 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 Trackback message. The TrackbackClient has a SendAsync call in progress. \n\r Message payload: {0}", message));
            }

            //------------------------------------------------------------
            //	Set async operation tracking indicators
            //------------------------------------------------------------
            this.SendOperationInProgress    = true;
            this.AsyncSendHasBeenCancelled  = false;

            //------------------------------------------------------------
            //	Build Trackback web request used to send the Trackback ping request
            //------------------------------------------------------------
            asyncHttpWebRequest = TrackbackClient.CreateWebRequest(this.Host, this.UserAgent, message, this.UseDefaultCredentials, this.clientOptions);

            //------------------------------------------------------------
            //	Get the async response to the web request
            //------------------------------------------------------------
            object[] state      = new object[6] { asyncHttpWebRequest, this, this.Host, message, this.clientOptions, userToken };
            IAsyncResult result = asyncHttpWebRequest.BeginGetResponse(new AsyncCallback(AsyncSendCallback), state);

            //------------------------------------------------------------
            //  Register the timeout callback
            //------------------------------------------------------------
            ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(AsyncTimeoutCallback), state, this.Timeout, true);
        }
コード例 #15
0
ファイル: TrackbackClient.cs プロジェクト: Jiyuu/Argotic
        /// <summary>
        /// Sends the specified message to a Trackback server to execute an Trackback ping request.
        /// </summary>
        /// <param name="message">A <see cref="TrackbackMessage"/> that represents the information needed to execute the Trackback ping request.</param>
        /// <returns>A <see cref="TrackbackResponse"/> that represents the server's response to the Trackback ping request.</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="TrackbackClient"/> has a <see cref="SendAsync(TrackbackMessage, Object)"/> call in progress.</exception>
        public TrackbackResponse Send(TrackbackMessage message)
        {
            //------------------------------------------------------------
            //	Local members
            //------------------------------------------------------------
            TrackbackResponse response   = null;

            //------------------------------------------------------------
            //	Validate parameter
            //------------------------------------------------------------
            Guard.ArgumentNotNull(message, "message");

            //------------------------------------------------------------
            //	Validate client state
            //------------------------------------------------------------
            if(this.Host == null)
            {
                throw new InvalidOperationException(String.Format(null, "Unable to send Trackback 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 Trackback message. The TrackbackClient has a SendAsync call in progress. \n\r Message payload: {0}", message));
            }

            //------------------------------------------------------------
            //	Execute the Trackback ping request
            //------------------------------------------------------------
            WebRequest webRequest   = TrackbackClient.CreateWebRequest(this.Host, this.UserAgent, message, this.UseDefaultCredentials, this.clientOptions);

            using (WebResponse webResponse = (WebResponse)webRequest.GetResponse())
            {
                response    = new TrackbackResponse(webResponse);
            }

            return response;
        }
コード例 #16
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TrackbackMessageSentEventArgs"/> 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="TrackbackMessage"/> that represents the Trackback ping request payload.</param>
        /// <param name="response">An <see cref="TrackbackResponse"/> that represents the response to the Trackback ping request.</param>
        /// <param name="credentials">A <see cref="ICredentials"/> that represents the authentication credentials utilized by the client when making the Trackback ping request. 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 Trackback ping request. 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 TrackbackMessageSentEventArgs(Uri host, TrackbackMessage message, TrackbackResponse 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;
        }