import javax.xml.soap.*; import java.io.*; import java.nio.charset.Charset; public class TestUtf16 { private static final String CHARSET = "UTF-16"; private static final String MESSAGE = "Hello"; public static void main(String args[]) { try { System.setProperty("javax.xml.transform.TransformerFactory", "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl"); ByteArrayInputStream inputStreamForString = getInputStreamForString(MESSAGE, Charset.forName(CHARSET)); SOAPMessage message = createSOAPMessage(inputStreamForString); System.out.println(getResponseAsString(message)); SOAPPart soapPart = message.getSOAPPart(); // TODO The call soapPart.getEnvelope() below fails. SOAPEnvelope envelope = soapPart.getEnvelope(); SOAPBody body = envelope.getBody(); System.out.println(body); } catch (Exception e) { e.printStackTrace(); } } private static String getResponseAsString(SOAPMessage response) throws SOAPException, IOException { if (response == null) return null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); response.writeTo(baos); return baos.toString(); } private static SOAPMessage createSOAPMessage(InputStream is) { SOAPMessage message = null; MimeHeaders headers = new MimeHeaders(); // TODO Comment this line to make the test pass. headers.addHeader("Content-Type", "text/xml; charset=" + CHARSET); try { MessageFactory messageFactory = MessageFactory.newInstance(); message = messageFactory.createMessage(headers, is); message.saveChanges(); } catch (Exception e) { e.printStackTrace(); message = null; } return message; } private static ByteArrayInputStream getInputStreamForString(String request, Charset cs) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); OutputStreamWriter osw; if (cs != null) { osw = new OutputStreamWriter(bos, cs); } else { osw = new OutputStreamWriter(bos, Charset.forName("UTF-8")); } osw.write(request); osw.flush(); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); return bis; } }