protected override List <Category> ParseResponseContent(XDocument xDoc) { XElement intElement = null; try { intElement = xDoc.Descendants(XmlRPCResponseConstants.STRING).First(); } catch (Exception e) { intElement = xDoc.Descendants(XmlRPCResponseConstants.INT).First(); } int categoryId = 0; if (int.TryParse(intElement.Value, out categoryId)) { Category.CategoryId = categoryId; List <Category> result = new List <Category>(); result.Add(Category); return(result); } else { Exception exception = new XmlRPCParserException(XmlRPCResponseConstants.XELEMENTMISSINGCHILDELEMENTS_CODE, XmlRPCResponseConstants.XELEMENTMISSINGCHILDELEMENTS_MESSAGE); throw exception; } }
// Executes when the user navigates to this page. protected override void OnNavigatedTo(NavigationEventArgs e) { // base.OnNavigatedTo(e); this.DebugLog("OnNavigatedTo"); if (Exception != null) { if (Exception is WordPress.Model.XmlRPCParserException) { //translate the exception message here WordPress.Model.XmlRPCParserException exp = Exception as WordPress.Model.XmlRPCParserException; if (exp.FaultCode == XmlRPCResponseConstants.SERVER_RETURNED_INVALID_XML_RPC_CODE) { ErrorText.Text = _localizedStrings.Prompts.ServerReturnedInvalidXmlRpcMessage; } else if (exp.FaultCode == XmlRPCResponseConstants.XML_RPC_OPERATION_FAILED_CODE) { ErrorText.Text = _localizedStrings.Prompts.XmlRpcOperationFailed; } else if (exp.FaultCode == XmlRPCResponseConstants.XELEMENTMISSINGCHILDELEMENTS_CODE) { ErrorText.Text = _localizedStrings.Prompts.XeElementMissing; } else { ErrorText.Text = Exception.Message; } } else { ErrorText.Text = Exception.Message; } fullStackTrace = Exception.ToString(); } else //user re-activaed the app { //look for transient data stored in the State dictionary if (State.ContainsKey(ERROR_DESC_VALUE)) { ErrorText.Text = State[ERROR_DESC_VALUE] as string; } if (State.ContainsKey(FULL_STACK_VALUE)) { fullStackTrace = State[FULL_STACK_VALUE] as string; } } }
protected override List <Comment> ParseResponseContent(XDocument xDoc) { XElement booleanElement = xDoc.Descendants(XmlRPCResponseConstants.BOOLEAN).First(); bool success = Convert.ToBoolean(Convert.ToInt16(booleanElement.Value)); if (success) { List <Comment> result = new List <Comment>(); result.Add(Comment); return(result); } else { Exception exception = new XmlRPCParserException(XmlRPCResponseConstants.XELEMENTMISSINGCHILDELEMENTS_CODE, XmlRPCResponseConstants.XELEMENTMISSINGCHILDELEMENTS_MESSAGE); throw exception; } }
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); }
protected XDocument cleanAndParseServerResponse(String responseContent) { if (String.IsNullOrEmpty(responseContent)) { throw new XmlRPCParserException(XmlRPCResponseConstants.SERVER_RETURNED_INVALID_XML_RPC_CODE, XmlRPCResponseConstants.SERVER_RETURNED_INVALID_XML_RPC_MESSAGE); } //responseContent += "<<"; //this.DebugLog("XML-RPC response: " + responseContent); string originalServerResponse = String.Copy(responseContent); //Keep copy of the server response "as-is", without cleaning it. Exception originalException = null; // If the recovery fails we should throw the original exception. if (!responseContent.StartsWith("<?xml")) { //clean the junk b4 the xml preamble this.DebugLog("cleaning the junk before the xml preamble"); int indexOfFirstLt = responseContent.IndexOf("<?xml"); if (indexOfFirstLt > -1) { responseContent = responseContent.Substring(indexOfFirstLt); } } XDocument xDoc = null; try { xDoc = XDocument.Parse(responseContent, LoadOptions.None); } catch (Exception ex) { //something went wrong during the parsing process. this.DebugLog("Parser error: " + ex.Message); //this is the original error, that should not be shown to the user. //Keep track of the original exception by adding the response from the server. If the recovery fails we should throw this exception originalException = new Exception("\n Server Response --> " + originalServerResponse, ex); xDoc = null; } if (xDoc != null) { return(xDoc); } //Start the recovery process if the parser failed. //Remove UTF-8 characters that may not appear in well-formed XML documents string pattern = @"#x((10?|[2-F])FFF[EF]|FDD[0-9A-F]|[19][0-9A-F]|7F|8[0-46-9A-F]|0?[1-8BCEF])"; Regex regex = new Regex(pattern, RegexOptions.IgnoreCase); if (regex.IsMatch(responseContent)) { this.DebugLog("found characters that must not appear in the XML-RPC response"); responseContent = regex.Replace(responseContent, String.Empty); } //Add here additional cleaning functions try { xDoc = XDocument.Parse(responseContent, LoadOptions.None); } catch (Exception) { //If the parser failed again, try to fix the response document. if (responseContent.Contains("<fault>")) { int startIndex = responseContent.IndexOf("<struct>"); int lastIndex = responseContent.LastIndexOf("</struct>"); if (startIndex != -1 && lastIndex != -1 && startIndex < lastIndex) { responseContent = "<methodResponse><fault><value>" + responseContent.Substring(startIndex, lastIndex - startIndex) + "</struct></value></fault></methodResponse>"; } } else { int startIndex = responseContent.IndexOf("<value>"); int lastIndex = responseContent.LastIndexOf("</value>"); if (startIndex != -1 && lastIndex != -1 && startIndex < lastIndex) { responseContent = "<methodResponse><params><param>" + responseContent.Substring(startIndex, lastIndex - startIndex) + "</value></param></params></methodResponse>"; } } //Try to re-parse the content once again xDoc = null; try { xDoc = XDocument.Parse(responseContent, LoadOptions.None); } catch (Exception) { //We're sorry but the server replied with a wrong XML-RPC document that the app can't recover //Original Exception should be thrown here. Exception exception = new XmlRPCParserException(XmlRPCResponseConstants.SERVER_RETURNED_INVALID_XML_RPC_CODE, XmlRPCResponseConstants.SERVER_RETURNED_INVALID_XML_RPC_MESSAGE, originalException); throw exception; } } return(xDoc); }