繁体   English   中英

格式化Web服务响应

[英]Formatting Web Service Response

我使用以下函数来检索Web服务响应:

private String getSoapResponse (String url, String host, String encoding, String soapAction, String soapRequest) throws MalformedURLException, IOException, Exception {         
    URL wsUrl = new URL(url);     
    URLConnection connection = wsUrl.openConnection();     
    HttpURLConnection httpConn = (HttpURLConnection)connection;     
    ByteArrayOutputStream bout = new ByteArrayOutputStream(); 

    byte[] buffer = new byte[soapRequest.length()];     
    buffer = soapRequest.getBytes();     
    bout.write(buffer);     
    byte[] b = bout.toByteArray();          

    httpConn.setRequestMethod("POST");
    httpConn.setRequestProperty("Host", host);

    if (encoding == null || encoding == "")
        encoding = UTF8;

    httpConn.setRequestProperty("Content-Type", "text/xml; charset=" + encoding);
    httpConn.setRequestProperty("Content-Length", String.valueOf(b.length));
    httpConn.setRequestProperty("SOAPAction", soapAction);

    httpConn.setDoOutput(true);
    httpConn.setDoInput(true);

    OutputStream out = httpConn.getOutputStream();
    out.write(b); 
    out.close();

    InputStreamReader is = new InputStreamReader(httpConn.getInputStream());
    StringBuilder sb = new StringBuilder();
    BufferedReader br = new BufferedReader(is);
    String read = br.readLine();

    while(read != null) {
        sb.append(read);
        read = br.readLine();
    }

    String response = decodeHtmlEntityCharacters(sb.toString());    

    return response = decodeHtmlEntityCharacters(response);
}

但是我对这段代码的问题是它返回了许多特殊字符并使XML的结构无效。
响应示例:

<PLANT>A565</PLANT>
          <PLANT>A567</PLANT>
          <PLANT>A585</PLANT>
          <PLANT>A921</PLANT>
          <PLANT>A938</PLANT>
        </PLANT_GROUP>
      </KPI_PLANT_GROUP_KEYWORD>
      <MSU_CUSTOMERS/>
    </DU>
    <DU> 

所以为了解决这个问题,我使用下面的方法并传递整个响应来替换所有特殊字符及其相应的标点符号。

private final static Hashtable htmlEntitiesTable = new Hashtable();
static {
    htmlEntitiesTable.put("&","&");
    htmlEntitiesTable.put(""","\"");
    htmlEntitiesTable.put("&lt;","<");
    htmlEntitiesTable.put("&gt;",">");  
}

private String decodeHtmlEntityCharacters(String inputString) throws Exception {
    Enumeration en = htmlEntitiesTable.keys();

    while(en.hasMoreElements()){
        String key = (String)en.nextElement();
        String val = (String)htmlEntitiesTable.get(key);

        inputString = inputString.replaceAll(key, val);
    }

    return inputString;
}

但是出现了另一个问题。 如果响应包含此段&lt;VALUE&gt;&lt; 0.5 &lt;/VALUE&lt; &lt;VALUE&gt;&lt; 0.5 &lt;/VALUE&lt; 如果这将由方法评估,输出将是:

<VALUE>< 0.5</VALUE>

这使得XML的结构再次失效。 数据是正确且有效的“<0.5”但是在VALUE元素中使用它会导致XML结构出现问题。

你能帮忙解决这个问题吗? 也许我可以改进获得或建立响应的方式。 有没有更好的方法来调用和获取Web服务的响应?

如何处理包含“<”或“>”的元素?

你知道如何使用第三方开源库吗?

你应该尝试使用apache commons-lang:

StringEscapeUtils.unescapeXml(xml)

以下堆栈溢出帖子中提供了更多详细信息:

如何在java中unescape XML

文档:

http://commons.apache.org/proper/commons-lang/javadocs/api-release/index.html http://commons.apache.org/proper/commons-lang/userguide.html#lang3

您使用SOAP错误。

特别是,您不需要以下代码行:

     String response = decodeHtmlEntityCharacters(sb.toString());    

只需返回sb.toString() 为了$ DEITY的缘故,不要使用字符串方法来解析检索到的字符串,使用XML解析器或完整的SOAP堆栈......

>或<字符是否始终出现在值的开头? 然后你可以使用正则表达式来处理&gt;的情况。 或者&lt; 后跟一个数字(或点,就此而言)。

示例代码,假设其中使用的替换字符串不会出现在XML中的任何其他位置:

private String decodeHtmlEntityCharacters(String inputString) throws Exception {
    Enumeration en = htmlEntitiesTable.keys();

    // Replaces &gt; or &lt; followed by dot or digit (while keeping the dot/digit)
    inputString = inputString.replaceAll("&gt;(\\.?\\d)", "Valuegreaterthan$1");
    inputString = inputString.replaceAll("&lt;(\\.?\\d)", "Valuelesserthan$1");

    while(en.hasMoreElements()){
        String key = (String)en.nextElement();
        String val = (String)htmlEntitiesTable.get(key);

        inputString = inputString.replaceAll(key, val);
    }

    inputString = inputString.replaceAll("Valuelesserthan", "&lt;");
    inputString = inputString.replaceAll("Valuegreaterthan", "&gt;");

    return inputString;
}

请注意,最合适的答案(对每个人来说都更容易)是在发送方正确编码XML(这也会使我的解决方案无法使用BTW)。

很难应对所有情况,但是你可以通过假设任何少于空格的数据是数据来添加更多规则来覆盖最常见的规则,并且大于它的前面有一个空格是数据,需要再次编码。

private final static Hashtable htmlEntitiesTable = new Hashtable();
static {
    htmlEntitiesTable.put("&amp;","&");
    htmlEntitiesTable.put("&quot;","\"");
    htmlEntitiesTable.put("&lt;","<");
    htmlEntitiesTable.put("&gt;",">");  
}

private String decodeHtmlEntityCharacters(String inputString) throws Exception {
    Enumeration en = htmlEntitiesTable.keys();

    while(en.hasMoreElements()){
        String key = (String)en.nextElement();
        String val = (String)htmlEntitiesTable.get(key);

        inputString = inputString.replaceAll(key, val);
    }

    inputString = inputString.replaceAll("< ","&lt; ");       
    inputString = inputString.replaceAll(" >"," &gt;");       

    return inputString;
}

“>”未在XML中转义。 所以你不应该有这个问题。 关于'<',以下是我能想到的选项。

  1. 在Web响应中对包含特殊字符的文本使用CDATA。
  2. 通过撤消订单重写文本。 例如。 如果是<2,则将其更改为2> x。 '>'除非是CDATA的一部分,否则不会被转义。
  3. 使用XML响应中的其他属性或元素来指示“<”或“>”。
  4. 使用正则表达式查找以“<”开头并后跟字符串的序列,后跟结束标记的“<”。 并将其替换为您可以在以后解释和替换的某些代码或某些值。

此外,您不需要这样做:

String response = decodeHtmlEntityCharacters(sb.toString()); 

在处理完文本中的“<”符号后,您应该能够解析XML。

您可以使用站点来测试正则表达式。

为什么不序列化你的xml?它比你正在做的容易得多。

举个例子:

var ser = new XmlSerializer(typeof(MyXMLObject));
using (var reader = XmlReader.Create("http.....xml"))
{
     MyXMLObject _myobj = (response)ser.Deserialize(reader);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM