2010-11-14 134 views
0

我需要预先。我发送HTTP GET请求并解析HTML响应,如果HTML响应(html字符串)包含一些子字符串,我想发送到客户端应用程序(WPF应用程序)一些错误/警告消息。WPF,处理错误和警告消息

在我的解决方案中,如果html字符串包含一些子字符串,我会抛出新的异常,这是愚蠢的,什么解决方案适合这个问题?

代码是在这里:如果

class MyClass 
{ 
//..... 

private bool SendRp(string postData) 
    { 
    bool result = false; 

    const string parameters = @"&lok=1&rpI=3"; 
    string htmlStringResult = HttpPostReq(
     new Uri(string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", PokecUrl.Rp, Account.SessionId, parameters)), postData); 
    try 
    { 
     if (htmlStringResult.ToLower(new CultureInfo("sk-Sk")).Contains("is is empty")) 
     { 
     throw new ArgumentException("ID is empty!"); 
     } 
     if (htmlStringResult.ToLower(new CultureInfo("sk-Sk")).Contains("id does not exist")) 
     { 
     throw new ArgumentException("ID does not exist."); 
     } 
     if (htmlStringResult.ToLower(new CultureInfo("sk-Sk")).Contains("blocked")) 
     { 
     throw new WebException("Your ID is blocked!"); 
     } 
     if (!htmlStringResult.ToLower(new CultureInfo("sk-Sk")).Contains("message was send")) 
     { 
     Match match = Regex.Match(htmlStringResult, @"\bhs=\w{15}\b"); 

     if (match.Success) 
     { 
      result = true; 
     } 
     else 
     { 
      throw new Exception("Some problem"); 
     } 

     } 
     return result; 
    } 
    catch (Exception exception) 
    { 
     throw exception; 
    } 
    } 

} 

回答

0

不知道我的理解什么是你的问题在这里。可以在您的“Web请求者”类上抛出异常,然后捕获UI类中的异常以显示消息。

显示一条消息最简单的方法是使用MessageBox

MessageBox.Show("Hello MessageBox"); 
+0

我想使用CodeContract。 http://blogs.msdn.com/b/hakoman/archive/2010/11/02/code-contracts-a-brilliant-way-to-do-validation-in-your-code-and-even-more。 ASPX。 – 2010-11-15 19:25:26