jeudi 26 mars 2015

How to code soap client in java?



I have and xml



<soap:Envelope xmlns:soap="http://ift.tt/sVJIaE" xmlns:xsi="http://ift.tt/ra1lAU" xmlns:xsd="http://ift.tt/tphNwY">
<soap:Body>
<SendRequest xmlns="http://tempuri.org/">
<auth xmlns="">
<Login xmlns="http://tempuri.org/">vcm</Login>
<Password xmlns="http://tempuri.org/">vcm</Password>
</auth>
<Request xmlns="">
<Request_code xmlns="http:// tempuri.org/">1</Request_code>
<Message_Code xmlns="http://tempuri.org/">1111</Message_Code>
<Params xmlns="http:// tempuri.org/">
<RequestParameter>
<Name />
<Value />
</RequestParameter>
</Params>
</Request>
</SendRequest>
</soap:Body>
</soap:Envelope>


And Java code



MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();

// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.removeAttribute("xmlns:SOAP-ENV");
envelope.setPrefix("soap");
envelope.addNamespaceDeclaration("xsd", "http://ift.tt/tphNwY");
envelope.addNamespaceDeclaration("xsi", "http://ift.tt/ra1lAU");

MimeHeaders mimeheaders = soapMessage.getMimeHeaders();
mimeheaders.addHeader("SOAPAction", "SendRequest");
SOAPHeader header = soapMessage.getSOAPHeader();
header.detachNode();

SOAPBody soapBody = envelope.getBody();
soapBody.setPrefix("soap");
SOAPElement sendRequest = soapBody.addChildElement("SendRequest");
SOAPElement auth = sendRequest.addChildElement("auth");
auth.addChildElement("Login")
.addTextNode("vcm");
auth.addChildElement("Password")
.addTextNode("vcm");

SOAPElement request = sendRequest.addChildElement("Request");
request.addChildElement("Request_code")
.addTextNode("1");
request.addChildElement("Message_Code")
.addTextNode("1111");
request.addChildElement("Params");
sendRequest.addAttribute(new QName("xmlns"), "http://tempuri.org/");
soapMessage.saveChanges();

/* Print the request message */
System.out.print("Request SOAP Message:");
soapMessage.writeTo(System.out);
System.out.println();


I console I see the tag without "http://tempuri.org/". And if I'm writing, for example,



sendRequest.addAttribute(new QName("aaa"), "http://tempuri.org/")


I'm having



<SendRequest aaa="http://tempuri.org/">


How to write java code?


Thanks for your help!




Aucun commentaire:

Enregistrer un commentaire