Get xml content of server response 500 (internal server error)

I am trying to consume a web service using a client generated using WSDL. All my posts are fine without any problem (SOAPFault can be caught and returned error accordingly), except that for a certain operation, for example when using incorrect credentials (so far defined), it returns a 500 response and SOAPFault XML together with him. I checked and saw the Fault XML file using SoapUI application. I can confirm that the XSD for this SOAPFault XML is the same as for the other (good) failures.

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

So this is what I am currently doing,

      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 posted along with Server Error 500?

Note. I don't have much control over the back end to change any parameters. What I intend to do is catch as normal exception, read the xml flaw posted and read the error message and return accordingly.

Thanks in advance.

[update] This is an example error string I am getting along with Server Response 500. (this is not related to 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 the SOAPFault exception expects 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 it even possible to read this SOAP envelope?

+3


source to share


1 answer


It took me a while, but I found a solution. Hope this helps others who are facing similar problems. First define it as WebException. Then you can get 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);
                 }
             }                  
       }

      

+1


source







All Articles