我发送一些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 '<'
我认为这可能是因为these predefined entities,但我尝试过的所有其他特殊角色都没有造成任何问题。
你知道为什么会这样吗,以及如何修复它(除了捕捉&
和<
的特定情况之外)?
我找到了建议的解决方案
httpPost.setEntity(new UrlEncodedFormEntity(List<NameValuePair>, String encoding))
但我没有NameValuePair
,只有StringEntity
。
答案 0 :(得分:1)
我认为可能是因为这些预定义的实体
完全。那些应该被转义为值,因此数据的正确版本将是:
[XML THAT DOESN'T WORK WITH '&' AND '<']