Exemplo n.º 1
0
        protected override List <Post> ParseResponseContent(XDocument xDoc)
        {
            XElement booleanElement = xDoc.Descendants(XmlRPCResponseConstants.BOOLEAN).First();

            if (XmlRPCResponseConstants.FALSE_STRING != booleanElement.Value)
            {
                List <Post> result = new List <Post>();
                result.Add(Page);
                return(result);
            }
            else
            {
                XmlRPCException exception = new XmlRPCException(XmlRPCResponseConstants.XML_RPC_OPERATION_FAILED_CODE, XmlRPCResponseConstants.XML_RPC_OPERATION_FAILED_MESSAGE);
                throw exception;
            }
        }
        private void OnBeginFileResponseCompleted(IAsyncResult result)
        {
            State state = result.AsyncState as State;

            if (retryCount == 0)
            {
                originalState = state;
            }

            if (IsCancelled)
            {
                CompletionMethod(null, null, true, originalState.Operation);
                return;
            }

            HttpWebRequest  request  = state.Request;
            HttpWebResponse response = null;

            try
            {
                response = request.EndGetResponse(result) as HttpWebResponse;
            }
            catch (Exception ex)
            {
                if (retryCount == 2)
                {
                    CompletionMethod(null, ex, false, originalState.Operation);
                    return;
                }
                else
                {
                    retryCount++;
                    AsyncOperation newAsyncOp = AsyncOperationManager.CreateOperation(Guid.NewGuid());
                    BeginBuildingHttpWebRequest(newAsyncOp);
                    return;
                }
            }

            lock (_syncRoot)
            {
                if (_isFinished == true)
                {
                    return;                      //Tentative idea on how to fix #209
                }
            }
            originalState.Operation.Post(onProgressReportDelegate, new ProgressChangedEventArgs(60, originalState.Operation.UserSuppliedState));


            string responseContent = null;

            using (Stream responseStream = response.GetResponseStream())
            {
                try
                {
                    using (StreamReader reader = new StreamReader(responseStream))
                    {
                        responseContent = reader.ReadToEnd();
                    }
                }
                catch (Exception ex)
                {
                    CompletionMethod(null, ex, false, originalState.Operation);
                    return;
                }
            }

            lock (_syncRoot)
            {
                if (_isFinished == true)
                {
                    return;                      //Tentative idea on how to fix #209
                }
            }
            originalState.Operation.Post(onProgressReportDelegate, new ProgressChangedEventArgs(80, originalState.Operation.UserSuppliedState));

            XDocument xDoc = null;

            try
            {
                xDoc = this.cleanAndParseServerResponse(responseContent);
            }
            catch (Exception ex2)
            {
                CompletionMethod(null, ex2, false, originalState.Operation);
                return;
            }

            var fault = xDoc.Descendants().Where(element => XmlRPCResponseConstants.NAME == element.Name && XmlRPCResponseConstants.FAULTCODE_VALUE == element.Value);

            if (null != fault && 0 < fault.Count())
            {
                Exception exception = ParseFailureInfo(xDoc.Descendants(XmlRPCResponseConstants.STRUCT).First());
                CompletionMethod(null, exception, false, originalState.Operation);
            }
            else
            {
                List <Media> items     = null;
                Exception    exception = null;
                try
                {
                    items = ParseResponseContent(xDoc);
                }
                catch (Exception ex)
                {
                    if (ex is XmlRPCException || ex is XmlRPCParserException)
                    {
                        exception = ex;
                    }
                    else
                    {
                        exception = new XmlRPCException(XmlRPCResponseConstants.SERVER_RETURNED_INVALID_XML_RPC_CODE, XmlRPCResponseConstants.SERVER_RETURNED_INVALID_XML_RPC_MESSAGE, ex);
                    }
                }
                CompletionMethod(items, exception, false, originalState.Operation);
            }
        }
        protected void OnBeginGetResponseCompleted(IAsyncResult result)
        {
            State           state    = result.AsyncState as State;
            HttpWebRequest  request  = state.Request;
            HttpWebResponse response = null;

            if (IsCancelled)
            {
                CompletionMethod(null, null, true, state.Operation);
                return;
            }

            try
            {
                response = request.EndGetResponse(result) as HttpWebResponse;
            }
            catch (Exception ex)
            {
                CompletionMethod(null, ex, false, state.Operation);
                return;
            }

            state.Operation.Post(onProgressReportDelegate, new ProgressChangedEventArgs(60, state.Operation.UserSuppliedState));

            Stream responseStream  = response.GetResponseStream();
            string responseContent = null;

            try
            {
                using (StreamReader reader = new StreamReader(responseStream))
                {
                    responseContent = reader.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                CompletionMethod(null, ex, false, state.Operation);
                return;
            }

            if (IsCancelled)
            {
                CompletionMethod(null, null, true, state.Operation);
                return;
            }

            state.Operation.Post(onProgressReportDelegate, new ProgressChangedEventArgs(80, state.Operation.UserSuppliedState));

            XDocument xDoc = null;

            try
            {
                xDoc = this.cleanAndParseServerResponse(responseContent);
            }
            catch (Exception ex2)
            {
                CompletionMethod(null, ex2, false, state.Operation);
                return;
            }

            var fault = xDoc.Descendants().Where(element => XmlRPCResponseConstants.NAME == element.Name && XmlRPCResponseConstants.FAULTCODE_VALUE == element.Value);

            if (null != fault && 0 < fault.Count())
            {
                Exception exception = ParseFailureInfo(xDoc.Descendants(XmlRPCResponseConstants.STRUCT).First());
                CompletionMethod(null, exception, false, state.Operation);
            }
            else
            {
                List <T>  items     = null;
                Exception exception = null;
                try
                {
                    items = ParseResponseContent(xDoc);
                }
                catch (Exception ex)
                {
                    if (ex is XmlRPCException || ex is XmlRPCParserException)
                    {
                        exception = ex;
                    }
                    else
                    {
                        exception = new XmlRPCException(XmlRPCResponseConstants.SERVER_RETURNED_INVALID_XML_RPC_CODE, XmlRPCResponseConstants.SERVER_RETURNED_INVALID_XML_RPC_MESSAGE, ex);
                    }
                }
                CompletionMethod(items, exception, false, state.Operation);
            }
        }