protected ResultSet ParseResult(string cgiResultString) { var result = new ResultSet(); if(string.IsNullOrEmpty(cgiResultString)) { result.State = ResultState.UnknownError; return result; } var values = new Dictionary<string, string>(); var xDoc = XDocument.Parse(cgiResultString); xDoc.Element("CGI_Result").Elements().ToList().ForEach(x => { var key = x.Name.ToString(); var value = x.Value; values.Add(key, value); }); var stateString = string.Empty; if(values.TryGetValue("result", out stateString)) { var state = ResultState.None; if(Enum.TryParse<ResultState>(stateString, out state)) { result.State = state; values.Remove("result"); } } result.Values = values; return result; }
protected ResultSet ExecuteCommand(string url) { var result = new ResultSet(); var client = new WebClient(); try { using (var stream = new StreamReader(client.OpenRead(url))) { var tmp = stream.ReadToEnd(); result = ParseResult(tmp); } } catch(Exception ex) { throw new CamException("An unspecified Error while processing is occured.", ex); } if(result.State == ResultState.Success) return result; else if(result.State == ResultState.AccessDeny) throw new CamException("Access denied"); else if(result.State == ResultState.CGIExecuteFail) throw new CamException("CGI Execution failed"); else if(result.State == ResultState.RequestFormatError) throw new CamException("Parameters Format Error"); else if(result.State == ResultState.Timeout) throw new CamException("An Timeout is occured"); else if(result.State == ResultState.UserNameOrPasswordWrong) throw new CamException("User or Password is wrong."); else if(result.State == ResultState.UnknownError) throw new CamException("An unknown Error is occured."); else throw new CamException("An unspecified Error is occured"); }