获取服务器响应500的xml内容(内部服务器错误)

时间:2015-05-21 08:56:38

标签: xml

我正在尝试使用使用其WSDL生成的客户端来使用Web服务。我的所有通信都没有问题(可以捕获SOAPFault并相应地返回错误),除了对于某些操作,例如使用错误的凭据(到目前为止已识别),它会返回响应500和SOAPFault XML。我检查过,我可以通过使用SoapUI应用程序看到Fault XML。我可以确认这个SOAPFault XML的XSD与其他(好的)错误相同。

由于服务器响应是500(我的假设),我无法将其作为SOAPException捕获,而是作为正常的异常。作为正常的异常,e.getMessage()不能帮助我知道错误究竟是什么。

所以,这就是我目前正在做的事情,

      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;

如何阅读与Server Error 500一起发送的此XML?

注意:我没有太多控制服务器端来修改任何参数。我打算做的是捕获作为正常异常,读取内发送的错误xml并读取错误消息并相应返回。

提前致谢。

[更新] 这是我与服务器响应500一起收到的示例故障字符串。(这个与凭证无关,但类似)

<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>

[更新] 我发现当服务器响应代码为500时,content-type设置为text / html,而SOAPFault异常则需要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>

现在,这甚至可以读取这个SOAP信封吗?

1 个答案:

答案 0 :(得分:1)

我花了一段时间,但我找到了解决方案。希望这会帮助那些陷入类似问题的人。 首先将其定义为WebException。然后,您可以从响应流中检索错误响应。

     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);
                 }
             }                  
       }