Reputation: 189
is there a way to consume a SOAP web service with java by just using:
I have an example request xml file by successfully consuming it with php but I can't find a proper way to do it on java.
[update: the web service's WSDL style is RPC/encoded]
[update #2: you can find how I solved the problem below (by using java stubs generated by IDEs)]
Upvotes: 0
Views: 9521
Reputation: 189
After a long search, I finally found a way to consume a rpc/encoded SOAP
web service.
I decided to generate the client stub from the wsdl url.
A successful way to do it is through this link (source : What is the easiest way to generate a Java client from an RPC encoded WSDL )
and after tweaking the generated code (of java stubs) by eclipse/netbeans you simple build your client. By using its classes you generated you can consume your preferred soap api.
e.g.
Auth auth = new Auth("username", "password");
SearchQuery fsq = new SearchQuery ("param1","param2","param3");
Model_SearchService service = new Model_SearchServiceLoc();
SearchRequest freq = new SearchRequest(auth, fsq);
Result r[] = service.getSearchPort().method(freq);
for(int i=0; i<r.length; i++){
System.out.println(i+" "+r[i].getInfo()[0].toString());
}
Upvotes: 1
Reputation: 42050
You can use java.net.HttpURLConnection
to send SOAP messages. e.g.:
public static void main(String[] args) throws Exception {
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" +
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\n" +
" <soap:Body>\r\n" +
" <ConversionRate xmlns=\"http://www.webserviceX.NET/\">\r\n" +
" <FromCurrency>USD</FromCurrency>\r\n" +
" <ToCurrency>CNY</ToCurrency>\r\n" +
" </ConversionRate>\r\n" +
" </soap:Body>\r\n" +
"</soap:Envelope>";
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username", "password".toCharArray());
}
});
URL url = new URL("http://www.webservicex.net/CurrencyConvertor.asmx");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setRequestProperty("SOAPAction", "http://www.webserviceX.NET/ConversionRate");
// Send the request XML
OutputStream outputStream = conn.getOutputStream();
outputStream.write(xml.getBytes());
outputStream.close();
// Read the response XML
InputStream inputStream = conn.getInputStream();
Scanner sc = new Scanner(inputStream, "UTF-8");
sc.useDelimiter("\\A");
if (sc.hasNext()) {
System.out.print(sc.next());
}
sc.close();
inputStream.close();
}
Upvotes: 5