我尝试从我写的客户端发送soap消息到服务器也是由我通过套接字编写的。
我将尝试仅粘贴应该执行此操作的重要代码(服务器和客户端之间的通信正常 - 我可以发送String对象):
/* Soap */
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage soapMsg = factory.createMessage();
SOAPPart part = soapMsg.getSOAPPart();
SOAPEnvelope envelope = part.getEnvelope();
SOAPHeader header = envelope.getHeader();
SOAPBody body = envelope.getBody();
header.addTextNode("SomeText");
header.setValue("SomeValue");
SOAPBodyElement element = body.addBodyElement(envelope.createName("JAVA", "training", "aaa"));
element.addChildElement("XXX").addTextNode("YYY");
SOAPBodyElement element1 = body.addBodyElement(envelope.createName("JAVA", "training", "bbb"));
element1.addChildElement("Spring").addTextNode("TrainingonSpring");
/* Soap */
String soapMessage = soapMsg.toString();
Socket s = new Socket(serverHostname, 10007);
PrintStream out = new PrintStream(s.getOutputStream(), true);
out.println(soapMessage);
ServerSocket serverSocket = new ServerSocket(10007);
Socket socket = serverSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader( socket.getInputStream()));
String inputLine = in.readLine();
// I've read Soap as a String and I want to change it to SoapMessage
InputStream is = new ByteArrayInputStream(inputLine.getBytes());
SOAPMessage soap = MessageFactory.newInstance().createMessage(null, is);
// I try to print for example Header Value to system output
System.out.println(soap.getSOAPHeader().getValue().toString());
当我尝试呼叫最后一行时,我在服务器中遇到了很多运行时错误。
ERROR: 'Content is not allowed in prolog.'
Nov 03, 2014 9:25:43 PM com.sun.xml.internal.messaging.saaj.soap.EnvelopeFactory createEnvelope
SEVERE: SAAJ0511: Unable to create envelope from given source
Exception in thread "main" com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Unable to create envelope from given source:
at com.sun.xml.internal.messaging.saaj.soap.EnvelopeFactory.createEnvelope(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.ver1_1.SOAPPart1_1Impl.createEnvelopeFromSource(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.SOAPPartImpl.getEnvelope(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.MessageImpl.getSOAPHeader(Unknown Source)
at EchoServer.main(EchoServer.java:64)
Caused by: javax.xml.transform.TransformerException: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
at com.sun.xml.internal.messaging.saaj.util.transform.EfficientStreamingTransformer.transform(Unknown Source)
... 5 more
答案 0 :(得分:0)
SOAPMessage
未定义toString()
,因此它继承自Object
,从而产生一个非类似SOAP的java对象ID。要从SOAPMessage
创建XML,请使用以下代码替换out.println(soapMessage);
:
soapMsg.writeTo(out);