如何拦截包含无效字符的Soap响应XML

huangapple go评论70阅读模式
英文:

How do I intercept a Soap Response XML with invalid character

问题

以下是您请求的翻译部分:

"The error is as follows: [com.ctc.wstx.exc.WstxLazyException] Illegal character entity: expansion character (code 0x1a\r\n at [row,col {unknown-source}]: [1,8035]

This is my WebService in Springboot with its WebMethod which receives an array of Polizas from which one of its fields contains an invalid character:"
(以下是错误消息,指出错误字符实体的问题。您描述了您的Springboot Web 服务,它包含一个 WebMethod,该方法接收一组 Polizas,其中的一个字段包含无效字符。)

(代码部分,不进行翻译)

"I have tried all kinds of interceptor (ClientInterceptor), filters (WebFilter), handler (SOAPHandler, HandlerInterceptor) and none have worked for me, the error occurs instantly and I have not found a way to obtain the XML response with the invalid character to be able to modify it and so everything works."
(我尝试了各种拦截器(ClientInterceptor)、过滤器(WebFilter)、处理程序(SOAPHandler、HandlerInterceptor),但都没有奏效,错误会立即发生,我也没有找到一种方法来获取包含无效字符的 XML 响应,以便能够修改它,使一切正常工作。)

"I'm beginning to think it's impossible to do, is there any other alternative that doesn't involve asking the response provider to correct it?. How can I intercept a XML response with invalid character (0x1a) without/before trigger a WebServiceException?"
(我开始觉得这是不可能做到的,是否有其他替代方案,而不必要求响应提供者来纠正它?如何拦截包含无效字符(0x1a)的 XML 响应,而不触发 WebServiceException?

"EDIT: Does anyone know if it is possible to use a FilterInputStream to correct the "XML" that comes with invalid characters before throwing an exception?"
(编辑:是否有人知道是否可以使用 FilterInputStream 来在抛出异常之前纠正带有无效字符的“XML”?)

希望这能帮助您解决问题。如果您有任何其他问题,请随时提问。

英文:

The error is as follows: [com.ctc.wstx.exc.WstxLazyException] Illegal character entity: expansion character (code 0x1a\r\n at [row,col {unknown-source}]: [1,8035]

This is my WebService in Springboot with its WebMethod which receives an array of Polizas from which one of its fields contains an invalid character:

@WebService(targetNamespace = "http://example.org.co/", name = "PolizaSoap")
@XmlSeeAlso({ObjectFactory.class})
public interface PolizaSoap {

@WebMethod(operationName = "Polizas", action = "http://example.org.co/Polizas")
@RequestWrapper(localName = "Polizas", targetNamespace = "http://example.org.co/", className = "com.example.Polizas")
@ResponseWrapper(localName = "PolizasResponse", targetNamespace = "http://example.org.co/", className = "com.example.PolizasResponse")
@WebResult(name = "PolizasResult", targetNamespace = "http://example.org.co/")
public ArrayOfPolizas Polizas(
    @WebParam(name = "param1", targetNamespace = "http://example.org.co/")
    java.lang.String param1,
    @WebParam(name = "param2", targetNamespace = "http://example.org.co/")
    java.lang.String param2,
    @WebParam(name = "param3", targetNamespace = "http://example.org.co/")
    java.lang.String param3
);
}

I have tried all kinds of interceptor (ClientInterceptor), filters (WebFilter), handler (SOAPHandler, HandlerInterceptor) and none have worked for me, the error occurs instantly and I have not found a way to obtain the XML response with the invalid character to be able to modify it and so everything works.

I'm beginning to think it's impossible to do, is there any other alternative that doesn't involve asking the response provider to correct it?. How can I intercept a XML response with invalid character (0x1a) without/before trigger a WebServiceException?

EDIT: Does anyone know if it is possible to use a FilterInputStream to correct the "XML" that comes with invalid characters before throwing an exception?

答案1

得分: 0

如果您调用外部SOAP服务并获得无效的XML作为响应,且没有机会让提供者修复其错误,那么您唯一的选择就是放弃标准的SOAP库,使用低级别的HTTP函数调用该服务,将响应视为纯文本并修复错误,然后自己解析修正后的XML。

以下是一个在不使用SOAP库的情况下调用SOAP服务的示例(摘自https://technology.amis.nl/soa/how-to-call-a-call-a-webservice-directly-from-java-without-webservice-library/ ):

public String getWeather(String city) throws MalformedURLException, IOException {
  //进行webservice HTTP请求的代码
  String responseString = "";
  String outputString = "";
  String wsURL = "http://www.deeptraining.com/webservices/weather.asmx";
  URL url = new URL(wsURL);
  URLConnection connection = url.openConnection();
  HttpURLConnection httpConn = (HttpURLConnection)connection;
  ByteArrayOutputStream bout = new ByteArrayOutputStream();
  String xmlInput =
    " <soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://litwinconsulting.com/webservices/\">\n" +
    " <soapenv:Header/>\n" +
    " <soapenv:Body>\n" +
    " <web:GetWeather>\n" +
    " <!--Optional:-->\n" +
    " <web:City>" + city + "</web:City>\n" +
    " </web:GetWeather>\n" +
    " </soapenv:Body>\n" +
    " </soapenv:Envelope>";
   
  byte[] buffer = new byte[xmlInput.length()];
  buffer = xmlInput.getBytes();
  bout.write(buffer);
  byte[] b = bout.toByteArray();
  String SOAPAction = "http://litwinconsulting.com/webservices/GetWeather";

  //设置适当的HTTP参数。
  httpConn.setRequestProperty("Content-Length",
  String.valueOf(b.length));
  httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
  httpConn.setRequestProperty("SOAPAction", SOAPAction);
  httpConn.setRequestMethod("POST");
  httpConn.setDoOutput(true);
  httpConn.setDoInput(true);
  OutputStream out = httpConn.getOutputStream();

  //将请求内容写入HTTP连接的输出流。
  out.write(b);
  out.close();
  //准备发送请求。
   
  //读取响应。
  InputStreamReader isr = new InputStreamReader(httpConn.getInputStream());
  BufferedReader in = new BufferedReader(isr);
   
  //将SOAP消息响应写入字符串。
  while ((responseString = in.readLine()) != null) {
    outputString = outputString + responseString;
  }
  
  //**************************************************************************************************** 
  //在这里,您将以纯文本形式获得响应,然后可以应用您的修正
  //**************************************************************************************************** 

  //将字符串输出解析为org.w3c.dom.Document,以便使用org.w3c.dom API访问每个节点。
  Document document = parseXmlFile(outputString);
  NodeList nodeLst = document.getElementsByTagName("GetWeatherResult");
  String weatherResult = nodeLst.item(0).getTextContent();
  System.out.println("Weather: " + weatherResult);
   
  return weatherResult;
}

这段代码演示了如何调用SOAP服务而不使用标准的SOAP库。

英文:

If you call an external SOAP service and you get invalid XML as a response and there is no chance of getting the provider to fix its error, then the only option you have is to forgo the standard SOAP libraries and call the service using low-level HTTP functions, treat the response as plain text and fix the error, and then parse the corrected XML yourself.

Here is an example to call a SOAP service without SOAP libraries (taken from https://technology.amis.nl/soa/how-to-call-a-call-a-webservice-directly-from-java-without-webservice-library/ )

public String getWeather(String city) throws MalformedURLException, IOException {
//Code to make a webservice HTTP request
String responseString = &quot;&quot;;
String outputString = &quot;&quot;;
String wsURL = &quot;http://www.deeptraining.com/webservices/weather.asmx&quot;;
URL url = new URL(wsURL);
URLConnection connection = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection)connection;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
String xmlInput =
&quot; &lt;soapenv:Envelope xmlns:soapenv=\&quot;http://schemas.xmlsoap.org/soap/envelope/\&quot; xmlns:web=\&quot;http://litwinconsulting.com/webservices/\&quot;&gt;\n&quot; +
&quot; &lt;soapenv:Header/&gt;\n&quot; +
&quot; &lt;soapenv:Body&gt;\n&quot; +
&quot; &lt;web:GetWeather&gt;\n&quot; +
&quot; &lt;!--Optional:--&gt;\n&quot; +
&quot; &lt;web:City&gt;&quot; + city + &quot;&lt;/web:City&gt;\n&quot; +
&quot; &lt;/web:GetWeather&gt;\n&quot; +
&quot; &lt;/soapenv:Body&gt;\n&quot; +
&quot; &lt;/soapenv:Envelope&gt;&quot;;
byte[] buffer = new byte[xmlInput.length()];
buffer = xmlInput.getBytes();
bout.write(buffer);
byte[] b = bout.toByteArray();
String SOAPAction = &quot;http://litwinconsulting.com/webservices/GetWeather&quot;;
// Set the appropriate HTTP parameters.
httpConn.setRequestProperty(&quot;Content-Length&quot;,
String.valueOf(b.length));
httpConn.setRequestProperty(&quot;Content-Type&quot;, &quot;text/xml; charset=utf-8&quot;);
httpConn.setRequestProperty(&quot;SOAPAction&quot;, SOAPAction);
httpConn.setRequestMethod(&quot;POST&quot;);
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
OutputStream out = httpConn.getOutputStream();
//Write the content of the request to the outputstream of the HTTP Connection.
out.write(b);
out.close();
//Ready with sending the request.
//Read the response.
InputStreamReader isr =
new InputStreamReader(httpConn.getInputStream());
BufferedReader in = new BufferedReader(isr);
//Write the SOAP message response to a String.
while ((responseString = in.readLine()) != null) {
outputString = outputString + responseString;
}
//**************************************************************************************************** 
// Here you have the response as plain text and you can apply your corrections
//**************************************************************************************************** 
//Parse the String output to a org.w3c.dom.Document and be able to reach every node with the org.w3c.dom API.
Document document = parseXmlFile(outputString);
NodeList nodeLst = document.getElementsByTagName(&quot;GetWeatherResult&quot;);
String weatherResult = nodeLst.item(0).getTextContent();
System.out.println(&quot;Weather: &quot; + weatherResult);
return weatherResult;
}

huangapple
  • 本文由 发表于 2023年2月19日 06:38:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496818.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定