title: Get the xml content of a Server response 500 (internal server error)
body: <p>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.</p>
  
  <p>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. </p>
  
  <p>So, this is what i am doing currently,</p>
  
  <pre><code>      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;
  </code></pre>
  
  <p>How can I read this XML sent together with Server Error 500? </p>
  
  <p>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.</p>
  
  <p>Thanks in advance.</p>
  
  <p>[update]
  This is a sample fault string I receive together with the Server Response 500. (this one not related to the credentials but similar)</p>
  
  <pre><code>&lt;SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"&gt;
     &lt;SOAP-ENV:Body&gt;
        &lt;SOAP-ENV:Fault&gt;
           &lt;faultcode&gt;SOAP-ENV:Server&lt;/faultcode&gt;
           &lt;faultstring&gt;null&lt;/faultstring&gt;
           &lt;detail&gt;
              &lt;IBResponse type="error"&gt;
                 &lt;DefaultTitle&gt;Response&lt;/DefaultTitle&gt;
                 &lt;StatusCode&gt;20&lt;/StatusCode&gt;
                 &lt;MessageID&gt;505&lt;/MessageID&gt;
                 &lt;DefaultMessage&gt;Unable to find a Routing corresponding to the incoming request message.&lt;/DefaultMessage&gt;
                 &lt;MessageParameters/&gt;
              &lt;/IBResponse&gt;
           &lt;/detail&gt;
        &lt;/SOAP-ENV:Fault&gt;
     &lt;/SOAP-ENV:Body&gt;
  &lt;/SOAP-ENV:Envelope&gt;
  </code></pre>
  
  <p>[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.</p>
  
  <pre><code>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
  &lt;?xml version="1.0"?&gt;
  &lt;SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"&gt;
    &lt;SOAP-ENV:Body&gt;
      &lt;SOAP-ENV:Fault&gt;
        &lt;faultcode&gt;SOAP-ENV:Server&lt;/faultcode&gt;
        &lt;faultstring&gt;null&lt;/faultstring&gt;
        &lt;detail&gt;
          &lt;IBResponse type="error"&gt;
            &lt;DefaultTitle&gt;Integration Broker Response&lt;/DefaultTitle&gt;
            &lt;StatusCode&gt;20&lt;/StatusCode&gt;
            &lt;MessageID&gt;535&lt;/MessageID&gt;
            &lt;DefaultMessage&gt;&lt;![CDATA[User Password required for Service Operation CI_SH_USERMAINT_CI_UP. (158,535)]]&gt;&lt;/DefaultMessage&gt;
            &lt;MessageParameters&gt;
              &lt;Parameter&gt;&lt;![CDATA[CI_SH_USERMAINT_CI_UP]]&gt;&lt;/Parameter&gt;
            &lt;/MessageParameters&gt;
          &lt;/IBResponse&gt;
        &lt;/detail&gt;
      &lt;/SOAP-ENV:Fault&gt;
    &lt;/SOAP-ENV:Body&gt;
  &lt;/SOAP-ENV:Envelope&gt;
  </code></pre>
  
  <p>So now, is this even possible to read this SOAP envelope?</p>