简体   繁体   中英

Sending SOAPMessage through sockets

I try to send soap message from client written by me to server also written by me through sockets.

I'll try to paste only important code that should do that (communication between server and client is ok - I can send String object):

Client

/* 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);

Server

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());

I have a lot of runtime errors in Server when I try to call last line.

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

SOAPMessage does not define toString() so that's inherited from Object , producing a decidedly un-SOAP-like java object id. To create XML from a SOAPMessage , replace out.println(soapMessage); with this code:

soapMsg.writeTo(out);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM