Vimal
Vimal

Reputation: 43

Get the xml content of a Server response 500 (internal server error)

I am trying to consume a web service using a client generated using its WSDL. All my communications are good with no problems (SOAPFault can be caught and error returned accordingly) except that for certain operation like when using wrong credentials (so far identified), it returns a response 500 and a SOAPFault XML together with it. I checked and I could see the Fault XML by using SoapUI application. I can confirm that the XSD for this SOAPFault XML is same as of the other (good) faults.

Since server response is 500 (my assumption), I am not able to catch this as a SOAPException but as normal Exception. As normal exception, e.getMessage() doesn't help me to know what the error exactly is.

So, this is what i am doing currently,

      try {
            outResponse = port.Service(input);

            if (condition = true)
                result = "SUCCESS";
            else
                result = "FAILURE";

        } catch (SOAPFaultException ex) {
            result = faultReader(ex);

            /* fault reader will strip the fault xml and get the error accordingly.*/

        } catch (Exception e){
            logger.info(e.getMessage());
            logger.info("Possible cause may be wrong user credentials.");

            /* I am expecting to read the SOAPFault XML here */

            result = "FAILURE";
        }
        return result;

How can I read this XML sent together with Server Error 500?

note: I do not have much control over the server side to modify any parameters. What I intend to do is to catch as a normal exception, read the fault xml sent within and read the error message and return accordingly.

Thanks in advance.

[update] This is a sample fault string I receive together with the Server Response 500. (this one not related to the credentials but similar)

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Body>
      <SOAP-ENV:Fault>
         <faultcode>SOAP-ENV:Server</faultcode>
         <faultstring>null</faultstring>
         <detail>
            <IBResponse type="error">
               <DefaultTitle>Response</DefaultTitle>
               <StatusCode>20</StatusCode>
               <MessageID>505</MessageID>
               <DefaultMessage>Unable to find a Routing corresponding to the incoming request message.</DefaultMessage>
               <MessageParameters/>
            </IBResponse>
         </detail>
      </SOAP-ENV:Fault>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

[update] I found that when the server response code is 500, the content-type is set to text/html whereas SOAPFault exception is expecting a text/xml.

HTTP/1.1 500 Internal Server Error
Date: Fri, 22 May 2015 06:30:40 GMT
Content-Length: 634
Content-Type: text/html
Connection: Close
<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Body>
    <SOAP-ENV:Fault>
      <faultcode>SOAP-ENV:Server</faultcode>
      <faultstring>null</faultstring>
      <detail>
        <IBResponse type="error">
          <DefaultTitle>Integration Broker Response</DefaultTitle>
          <StatusCode>20</StatusCode>
          <MessageID>535</MessageID>
          <DefaultMessage><![CDATA[User Password required for Service Operation CI_SH_USERMAINT_CI_UP. (158,535)]]></DefaultMessage>
          <MessageParameters>
            <Parameter><![CDATA[CI_SH_USERMAINT_CI_UP]]></Parameter>
          </MessageParameters>
        </IBResponse>
      </detail>
    </SOAP-ENV:Fault>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

So now, is this even possible to read this SOAP envelope?

Upvotes: 4

Views: 5697

Answers (1)

Shriom Manerker
Shriom Manerker

Reputation: 21

It took me a while but I found the solution. Hopefully this will help others who are stuck with similar problems. Firstly define it as WebException.Then you can retrieve the error response from the response stream.

     try
     {
         webResponse = (HttpWebResponse)req.GetResponse();
         statusCode = webResponse.StatusCode;
         Response.Write("Status code " + statusCode.ToString());

      }
      catch (WebException ex)
      {
          if (ex.Response != null)
            {
                StreamReader responseReader;

            using (responseReader = new StreamReader(ex.Response.GetResponseStream()))
                {
                    string exMessage = responseReader.ReadToEnd().ToString();
                    Response.Write("<br> Response from textkernel"+exMessage);
                 }
             }                  
       }

Upvotes: 2

Related Questions