0

我将一些 XML 作为 SOAP 请求发送,但是当该 XML 包含字符&<时,我得到一个空响应。以下是一些相关代码:

String myXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
               "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">" +
                   "<soap12:Body>" +
                      "[XML THAT DOESN'T WORK WITH '&' AND '<']" +
                   "</soap12:Body>" +
               "</soap12:Envelope>";

HttpPost httpPost = new HttpPost("http://website.com");
StringEntity stringEntity = new StringEntity(myXml, HTTP.UTF_8);
stringEntity.setContentType("text/xml");
httpPost.setHeader("Content-Type", "application/soap+xml;charset=UTF-8)";
httpPost.setEntity(stringEntity);

HttpClient httpClient = new DefaultHttpClient();
BasicHttpResponse httpResponse = (BasicHttpResponse) httpClient.execute(httpPost);

HttpEntity httpEntity = httpResponse.getEntity();
InputStream inputStream = responseEntity.getContent();
//Checking inputStream here reveals that it is empty (not null) when xml has '&' or '<'

我认为这可能是因为这些预定义的实体,但我尝试过的所有其他特殊字符都没有引起任何问题。

你知道这是为什么,以及我该如何解决它(除了捕捉&and的特定情况<)?

我找到了一个建议的解决方案

httpPost.setEntity(new UrlEncodedFormEntity(List<NameValuePair>, String encoding))

但我没有NameValuePairs,只有一个StringEntity.

4

1 回答 1

1

我认为可能是因为这些预定义的实体

确切地。这些应该被转义为值,因此正确版本的数据将是:

[XML THAT DOESN'T WORK WITH '&amp;' AND '&lt;']
于 2012-10-25T15:21:03.173 回答