0

is there a way to consume a SOAP web service with java by just using:

  • the SOAPaction required (for example methodname called "find")
  • the URL of the web service
  • header authentication (username and password)
  • in the end output the results

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)]

jsurf
  • 189
  • 3
  • 11
  • you searched google and couldn't find _any_ examples for consuming a soap webservice in java? – jtahlborn Sep 10 '14 at 14:43
  • I found plenty of articles and ibm posts too but the thing is that many suggest to use JAX-WS and wsimport tool but wsimport can't "use" rpc/encoded WSDL. Plus I found another way of doing if with HttpUrlConnection but I am only getting as a response the wsdl of the webserver same as when I visit the url of wsdl. Thanks for your help – jsurf Sep 10 '14 at 14:58
  • since the "rpc encoded" bit seems to be a pretty important part of the question, you should include that in the actual question, not just as a tag. – jtahlborn Sep 10 '14 at 16:39
  • i didn't downvote a "real problem", i downvoted a non-specific question which doesn't give much indication of any previous research into a topic which is heavily documented online. – jtahlborn Sep 10 '14 at 23:38

2 Answers2

5

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

}
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
  • Thank you for your answer @PaulVargas I found a solution to my problem by using the java stubs. – jsurf Sep 15 '14 at 23:01
1

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());
}
Community
  • 1
  • 1
jsurf
  • 189
  • 3
  • 11