title: How do I catch HTTP errors when using WCF's WebGet attribute?
body: <p>I'm using the [WebGet] attribute to query a REST API on a web server. Every so often (because the API is still under development) I get back an HTML document in the form of an HTTP/500 error instead of the XML I want.</p>
  
  <p>Here's an example of my operation contract:</p>
  
  <pre><code>[WebGet(UriTemplate = "search/?api_key={key}&amp;query={query}",
     BodyStyle = WebMessageBodyStyle.Bare)]
  [OperationContract]
  XElement Find(string key, string query);
  </code></pre>
  
  <p>... and here's how I call it:</p>
  
  <pre><code>var factory = new WebChannelFactory&lt;IFooFinder&gt;(
      new Uri("http://api.example.com"));
  var channel = factory.CreateChannel();
  var results = channel.Find(myApiKey, "foo");
  </code></pre>
  
  <p>In the event of an error, "results" ends up being an XElement that contains this XML:</p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;&lt;/head&gt;
      &lt;body&gt;
          500 Internal Server Error
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Is there some way to catch the 500 error before the XML is returned, and have the channel throw an exception or something? Or am I going to have to check the results variable each time to make sure the XML I expect is in there?</p>