I am sending some XML as a SOAP request, but when that XML contains the characters & or <, I get an empty response. Here is some relevant code:
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 '<'
I thought it might be because of these predefined entities, but every other special character that I tried it with caused no problems.
Do you know why this is, and how I can go about fixing it (other than just catching the specific cases of & and <)?
I found a suggested solution to do
httpPost.setEntity(new UrlEncodedFormEntity(List<NameValuePair>, String encoding))
But I don’t have NameValuePairs, just a StringEntity.
Exactly. Those should be escaped for values, so the correct version of the data would be: