コード例 #1
0
        private void FetchStats(AsyncOperation operation)
        {
            bool hasNetworkConnection = NetworkInterface.GetIsNetworkAvailable();

            if (!hasNetworkConnection)
            {
                Exception connErr = new NoConnectionException();
                CompletionMethod(null, connErr, false, operation);
                return;
            }

            HttpWebRequest request = HttpWebRequest.Create(Uri) as HttpWebRequest;

            request.AllowAutoRedirect = true;
            request.ContentType       = XmlRPCRequestConstants.CONTENTTYPE;
            request.Method            = XmlRPCRequestConstants.POST;
            request.UserAgent         = Constants.WORDPRESS_USERAGENT;

            request.BeginGetResponse(responseResult =>
            {
                try
                {
                    HttpWebResponse response = request.EndGetResponse(responseResult) as HttpWebResponse;
                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                    {
                        string responseContent = reader.ReadToEnd();
                        XDocument xDoc         = ParseDocument(responseContent);

                        Exception exception = null;
                        List <T> items      = null;

                        var fault = xDoc.Descendants().Where(element => XmlRPCResponseConstants.NAME == element.Name && XmlRPCResponseConstants.FAULTCODE_VALUE == element.Value);
                        if (null != fault && 0 < fault.Count())
                        {
                            exception = ParseFailureInfo(xDoc.Descendants(XmlRPCResponseConstants.STRUCT).First());
                        }
                        else
                        {
                            try
                            {
                                items = ParseResponseContent(xDoc);
                            }
                            catch (Exception ex)
                            {
                                exception = ex;
                            }
                        }

                        CompletionMethod(items, exception, false, operation);
                    }
                }
                catch (Exception ex)
                {
                    CompletionMethod(new List <T>(), ex, false, operation);
                }
            }, request);
        }
コード例 #2
0
        internal void BeginBuildingHttpWebRequest(AsyncOperation asyncOp)
        {
            bool hasNetworkConnection = NetworkInterface.GetIsNetworkAvailable();

            if (!hasNetworkConnection)
            {
                Exception connErr = new NoConnectionException();
                CompletionMethod(null, connErr, false, asyncOp);
                return;
            }

            if (IsCancelled)
            {
                CompletionMethod(null, null, true, asyncOp);
                return;
            }

            HttpWebRequest request;

            try
            {
                request = HttpWebRequest.CreateHttp(Url) as HttpWebRequest;
            }
            catch (Exception errorWhileCreatingConnection)
            {
                CompletionMethod(null, errorWhileCreatingConnection, false, asyncOp);
                return;
            }

            request.AllowAutoRedirect = true;
            request.ContentType       = XmlRPCRequestConstants.CONTENTTYPE;
            request.Method            = XmlRPCRequestConstants.POST;
            request.UserAgent         = Constants.WORDPRESS_USERAGENT;

            State state = new State {
                Operation = asyncOp, Request = request
            };

            request.BeginGetRequestStream(OnBeginGetRequestStreamCompleted, state);

            asyncOp.Post(onProgressReportDelegate, new ProgressChangedEventArgs(20, asyncOp.UserSuppliedState));
        }
コード例 #3
0
        private void RequestKey(AsyncOperation operation)
        {
            bool hasNetworkConnection = NetworkInterface.GetIsNetworkAvailable();

            if (!hasNetworkConnection)
            {
                Exception connErr = new NoConnectionException();
                CompletionMethod(null, connErr, false, operation);
                return;
            }

            HttpWebRequest request = HttpWebRequest.Create(Constants.WORDPRESS_APIKEY_URL) as HttpWebRequest;

            request.AllowAutoRedirect = true;
            request.ContentType       = XmlRPCRequestConstants.CONTENTTYPE;
            request.Method            = XmlRPCRequestConstants.POST;
            request.UserAgent         = Constants.WORDPRESS_USERAGENT;
            if (selfHosted)
            {
                request.Credentials = new NetworkCredential(blog.DotcomUsername, blog.DotcomPassword);
            }
            else
            {
                request.Credentials = new NetworkCredential(blog.Username, blog.Password);
            }


            request.BeginGetResponse(responseResult =>
            {
                try
                {
                    HttpWebResponse response = request.EndGetResponse(responseResult) as HttpWebResponse;
                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                    {
                        string responseContent = reader.ReadToEnd();
                        XDocument xDoc         = ParseDocument(responseContent);

                        Exception exception = null;
                        List <Blog> items   = null;

                        var fault = xDoc.Descendants().Where(element => XmlRPCResponseConstants.NAME == element.Name && XmlRPCResponseConstants.FAULTCODE_VALUE == element.Value);
                        if (null != fault && 0 < fault.Count())
                        {
                            exception = ParseFailureInfo(xDoc.Descendants(XmlRPCResponseConstants.STRUCT).First());
                        }
                        else
                        {
                            try
                            {
                                items = ParseResponseContent(xDoc);
                            }
                            catch (Exception ex)
                            {
                                exception = new XmlRPCParserException(XmlRPCResponseConstants.XELEMENTMISSINGCHILDELEMENTS_CODE, XmlRPCResponseConstants.XELEMENTMISSINGCHILDELEMENTS_MESSAGE, ex);
                            }
                        }

                        CompletionMethod(items, exception, false, operation);
                    }
                }
                catch (Exception ex)
                {
                    CompletionMethod(new List <Blog>(), ex, false, operation);
                }
            }, request);
        }