Get error message in Httpclient Post

Asked

Viewed 480 times

1

I have a WCF Service on a server whose service is also configured as Webinvoke:

[WebInvoke(
        Method = "POST",
        UriTemplate = "/work",
        BodyStyle = WebMessageBodyStyle.WrappedResponse,
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json)]

Consumption is successfully performed on a PCL project, however, depending on the information that is passed on, the service will handle and may trigger exceptions:

Service call:

HttpResponseMessage response = await client.PostAsync(uri, content);

Feedback information is read if response.IsSuccessStatusCode Don’t be true, I get it the following way:

var result = response.Content.ReadAsStringAsync().Result;

This result is a string from an html document, contains a lot of information, and the error message I’m looking for is in the body tag, precisely within a div whose id is equal to "content" (the code below).... how can I get only the message, some specific way?

result:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Request Error</title>
    <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
  </head>
  <body>
    <div id="content">
      <p class="heading1">Request Error</p>
      <p>The server encountered an error processing the request. The exception message is 'A base de dados Teste não pertence a empresa Alfalagos.'. See server logs for more details. The exception stack trace is: </p>
      <p>   at Custom.OuroNet.Server.WCFCadastre.Cadastro.DoWork(DTOMessageRequest messageRequest)
   at SyncInvokeDoWork(Object , Object[] , Object[] )
   at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]&amp; outputs)
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</p>
    </div>
  </body>
</html>

1 answer

1


Using the library Htmlagilitypack and using the Xpath to capture the div you can get the contents of it

HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
document.LoadHtml(html);
HtmlAgilityPack.HtmlNode node = document.DocumentNode;
HtmlAgilityPack.HtmlNode content = node.Descendants("div").
           Where(div => div.GetAttributeValue("id", string.Empty) == "content").
           FirstOrDefault();

See working on .Netfiddle

Obs: It is necessary to install the library Htmlagilitypack in your project via Nuget

  • Thanks for the help...but in Xamarin I had to use the Htmlagilitypack-PCL package, and it does not contain the option "Selectnodes".

  • I made the change to your case

  • 1

    It worked, thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.