我将一些 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))
但我没有NameValuePair
s,只有一个StringEntity
.