如何在Java中将字符串转换为SOAPMessage?

我想知道有没有办法将字符串转换为SOAPMessage

让我说我有一个字符串如下:

String send = "" + "" + "" + "" + sessionId + "" + "" + "" + "" + 11 + "" + "" + "" + "" + "" + "" + extime + "" + "0" + "" + time + "" + "1905" + "" + numSend + "" + "" + "" + numSend + "" + "" + "" + "Message body here." + "" + "" + "" + ""; 

我这样做的时候:

  SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance(); SOAPConnection connection = sfc.createConnection(); InputStream is = new ByteArrayInputStream(send.getBytes()); SOAPMessage request = MessageFactory.newInstance().createMessage(null, is); request.removeAllAttachments(); SOAPPart part = request.getSOAPPart(); part.detachNode(); SOAPEnvelope env = part.getEnvelope(); env.detachNode(); SOAPBody body = env.getBody(); body.detachNode(); SOAPHeader head = env.getHeader(); head.detachNode(); request.writeTo(System.out); URL endpoint = new URL("http://sdp.somewhere.com.tr/view/LbsOpaqueService.wsdl"); SOAPMessage response = connection.call(request, endpoint); System.out.println(response.getContentDescription()); 

一切正常。 但我仍然从服务器获得NULL 。 可能是什么原因? 我已经改变来逃避字符。

将String转换为输入流,然后将其读入SOAP消息工厂。

 InputStream is = new ByteArrayInputStream(send.getBytes()); SOAPMessage request = MessageFactory.newInstance().createMessage(null, is); 

你可以在这里阅读有关如何做到这一点的信息

这对我有用:

 SOAPMessage sm = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage(new MimeHeaders(), is); 

代替:

 System.out.println(response.getContentDescription()); 

获得响应后,将其替换为以下行:

 response.writeTo(System.out); 

这样你就可以从一个文件中读取:

 byte[] encoded = Files.readAllBytes(Paths.get("C:/resources/soap/RequestFileReply.xml")); InputStream bStream = new ByteArrayInputStream(encoded); SOAPMessage request = MessageFactory.newInstance().createMessage(null, bStream); SOAPBody body = request.getSOAPBody();