コード例 #1
0
        /// <summary>
        /// Call a method and return the XML result. Will throw an exception of type BlogClientException
        /// if an error occurs.
        /// </summary>
        /// <param name="postUrl"></param>
        /// <param name="methodName"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        protected XmlNode CallMethod(string methodName, params XmlRpcValue[] parameters)
        {
            string url = _postApiUrl;
            if (Options.SupportsHttps)
            {
                if (url.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
                    url = "https://" + url.Substring("http://".Length);
            }

            try
            {
                // create an RpcClient
                XmlRpcClient xmlRpcClient = new XmlRpcClient(url, ApplicationEnvironment.UserAgent, new HttpRequestFilter(BeforeHttpRequest), _clientOptions.CharacterSet);

                // call the method
                XmlRpcMethodResponse response = xmlRpcClient.CallMethod(methodName, parameters);

                // if success, return the response
                if (!response.FaultOccurred)
                {
                    return response.Response;
                }
                else // throw error indicating problem
                {
                    // prepare to throw exception
                    BlogClientException exception;

                    // allow underlying provider to return a more descriptive exception type
                    exception = ExceptionForFault(response.FaultCode, response.FaultString);

                    if (exception == null) // if it couldn't just go generic
                        exception = new BlogClientProviderException(response.FaultCode, response.FaultString);

                    // throw the exception
                    throw exception;
                }
            }
            catch (IOException ex)
            {
                throw new BlogClientIOException("calling XML-RPC method " + methodName, ex);
            }
            catch (WebException ex)
            {
                HttpRequestHelper.LogException(ex);

                // see if this was a 404 not found
                switch (ex.Status)
                {
                    case WebExceptionStatus.ProtocolError:
                        HttpWebResponse response = ex.Response as HttpWebResponse;
                        if (response != null && response.StatusCode == HttpStatusCode.NotFound)
                            throw new BlogClientPostUrlNotFoundException(url, ex.Message);
                        else if (response.StatusCode == HttpStatusCode.Unauthorized)
                            throw new BlogClientAuthenticationException(response.StatusCode.ToString(), ex.Message, ex);
                        else
                            throw new BlogClientHttpErrorException(url, string.Format(CultureInfo.InvariantCulture, "{0} {1}", (int)response.StatusCode, response.StatusDescription), ex);
                    default:
                        throw new BlogClientConnectionErrorException(url, ex.Message);
                }
            }
            catch (XmlRpcClientInvalidResponseException ex)
            {
                throw new BlogClientInvalidServerResponseException(methodName, ex.Message, ex.Response);
            }
        }
コード例 #2
0
ファイル: Blog.cs プロジェクト: gmilazzoitag/OpenLiveWriter
        private bool ErrorIsInvalidPostId(BlogClientProviderException ex)
        {
            string faultCodePattern = BlogClient.Options.InvalidPostIdFaultCodePattern;
            string faultStringPattern = BlogClient.Options.InvalidPostIdFaultStringPattern;

            if (faultCodePattern != String.Empty && faultStringPattern != String.Empty)
            {
                return FaultCodeMatchesInvalidPostId(ex.ErrorCode, faultCodePattern) &&
                       FaultStringMatchesInvalidPostId(ex.ErrorString, faultStringPattern);
            }
            else if (faultCodePattern != String.Empty)
            {
                return FaultCodeMatchesInvalidPostId(ex.ErrorCode, faultCodePattern);
            }
            else if (faultStringPattern != String.Empty)
            {
                return FaultStringMatchesInvalidPostId(ex.ErrorString, faultStringPattern);
            }
            else
            {
                return false;
            }
        }