Reputation: 426
I need to consume a SOAP Server named "Mouser" for my company. However I have a problem when I try to send a message.
The documentation of my request is :
POST /service/searchapi.asmx HTTP/1.1
Host: www.mouser.fr
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Header>
<MouserHeader xmlns="http://api.mouser.com/service">
<AccountInfo>
<PartnerID>string</PartnerID>
</AccountInfo>
</MouserHeader>
</soap12:Header>
<soap12:Body>
<SearchByPartNumber xmlns="http://api.mouser.com/service">
<mouserPartNumber>string</mouserPartNumber>
</SearchByPartNumber>
</soap12:Body>
</soap12:Envelope>
Ok, now I will how you my Java Code with the message that I send :
String mpns = "BAV99";
SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
SOAPMessage message = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage();
MimeHeaders mimeHeader = message.getMimeHeaders();
mimeHeader.setHeader("Content-Type", "application/soap+xml; charset=utf-8");
SOAPPart soapPart = message.getSOAPPart();
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
+ "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n"
+ " <soap12:Header>\n"
+ " <MouserHeader xmlns=\"http://api.mouser.com/service\">\n"
+ " <AccountInfo>\n"
+ " <PartnerID>" + key + "</PartnerID>\n"
+ " </AccountInfo>\n"
+ " </MouserHeader>\n"
+ " </soap12:Header>\n"
+ " <soap12:Body>\n"
+ " <SearchByPartNumber xmlns=\"http://api.mouser.com/service\">\n"
+ " <mouserPartNumber>" + mpns + "</mouserPartNumber>\n"
+ " </SearchByPartNumber>\n"
+ " </soap12:Body>\n"
+ "</soap12:Envelope>";
StreamSource source = new StreamSource(new StringReader(xml));
soapPart.setContent(source);
message.saveChanges();
System.out.println("Send : ");
message.writeTo(System.out);
System.out.println();
java.net.URL endpoint = new URL(targetUrl);
SOAPMessage reply = connection.call(message, endpoint);
StringWriter sw = new StringWriter();
TransformerFactory.newInstance().newTransformer().transform(new DOMSource(message.getSOAPPart()), new StreamResult(sw));
connection.close();
System.out.println("Received : ");
System.out.println(sw.toString());
return sw.toString();
Instead of get a the response that I want from the server I get the same message that I sent BUT with the new ATTRIBUTE : standalone = "no" What's does it means ? Why this response ?
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Header>
<MouserHeader xmlns="http://api.mouser.com/service">
<AccountInfo>
<PartnerID>key</PartnerID>
</AccountInfo>
</MouserHeader>
</soap12:Header>
<soap12:Body>
<SearchByPartNumber xmlns="http://api.mouser.com/service">
<mouserPartNumber>BAV99</mouserPartNumber>
</SearchByPartNumber>
</soap12:Body>
</soap12:Envelope>
Thanks for helping !
Upvotes: 3
Views: 1554
Reputation: 426
I found how to do ! Thanks everybody, an particularly at foolvoe99 because it's with your idea that's I knew where to search.
I used "wsimport" to generate java class from WSDL and used them. Here is how I did it, so this can help others :
URL wsdlLocation = new URL("your_wsdl_target");
QName apiName = new QName("your_service_target", "your_service_name");
your_service_name api = new your_service_name(wsdlLocation, apiName);
api.addPort(your_service_name, SOAPBinding.SOAP12HTTP_BINDING, "your_service_target/name");
QName port_name = new QName("your_service_target", "port_name");
Dispatch<SOAPMessage> disp = api.createDispatch(port_name, SOAPMessage.class, Service.Mode.MESSAGE);
String xml = "Your SOAP MESSAGE";
MessageFactory mf = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
SOAPMessage request = mf.createMessage();
SOAPPart part = request.getSOAPPart();
StreamSource source = new StreamSource(new StringReader(xml));
part.setContent(source);
request.saveChanges();
SOAPMessage response = disp.invoke(request);
StringWriter sw = new StringWriter();
TransformerFactory.newInstance().newTransformer().transform(new DOMSource(response.getSOAPPart()), new StreamResult(sw));
org.json.JSONObject xmlJSONObj = XML.toJSONObject(sw.toString());
return xmlJSONObj.toString(2);
Upvotes: 0
Reputation: 21
Actually you can generate class with soap ui. And your program can easily call the service using the class created without construct your own request header and body But you need some library. Example java jdk comes with jax-ws lib
tutorial: http://www.soapui.org/soap-and-wsdl/soap-code-generation.html
Upvotes: 2