2

What would be the equivalent of this short php code in java?

$client = new SoapClient(NULL,
  array(
    "location" => "http://hostname:port/')",
    "uri" => "urn:String",
    "style" => SOAP_RPC,
    'login' => "soapuser",
    'password' => "soappass",
  )
);


$command = "This command will be sent to SOAP";
try {
  $result = $client->executeCommand(new SoapParam($command, "command"));
  return true;
}
catch (Exception $e)  {
  return false;
}

is it possible to achieve the same result with a short java class ?

Resorath
  • 1,602
  • 1
  • 12
  • 31
Daniel S
  • 621
  • 3
  • 8
  • 18
  • Take a look at the resources linked from this answer: http://stackoverflow.com/a/16556532/1325237 -- if you go to the DZone tutorial and skip down to the WS Client section, you'll see an explanation of what you need to do to be able to invoke the service using code similar to what you have above. – Alex Sep 10 '13 at 10:01
  • duplicate of http://stackoverflow.com/questions/15948927/working-soap-client-example – Bass Jobsen Sep 11 '13 at 18:01
  • @ Bass Jobsen - i haven't found how to run this executeCommand in link you provided – Daniel S Sep 11 '13 at 18:29

3 Answers3

1

update 2 I don't understand your question maybe. (could you provide the wdsl of your servive?) To create a client like your php code:

use:

package com.mkyong.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.mkyong.ws.HelloWorld;

public class HelloWorldClient{

    public static void main(String[] args) throws Exception {

    URL url = new URL("http://localhost:9999/ws/hello?wsdl");

        //1st argument service URI, refer to wsdl document above
    //2nd argument is service name, refer to wsdl document above
        QName qname = new QName("http://ws.mkyong.com/", "HelloWorldImplService");

        Service service = Service.create(url, qname);

        HelloWorld hello = service.getPort(HelloWorld.class);

        System.out.println(hello.getHelloWorldAsString("mkyong"));

    }

}

copy this file to com/mkyong/client. To compile use javac com/mkyong/client/HelloWorldClient.java and to run use java com/mkyong/client/HelloWorldClient, see also: Compiling four java files within one package using javac and making a java package in the command line

"Mapped" to your php example http://localhost:9999/ws/hello?wsdl will be the equivalent of http://hostname:port/ and executeCommand will be the same as hello.getHelloWorldAsString.

update try JAX-WS (http://jax-ws.java.net/)

From http://www.mkyong.com/webservices/jax-ws/jax-ws-hello-world-example/:

package com.mkyong.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

//Service Endpoint Interface
@WebService
@SOAPBinding(style = Style.RPC)
public interface HelloWorld{

    @WebMethod String getHelloWorldAsString(String name);

}

Beside the answer here: Working Soap client example you could find many tutorials which tell you how to write a soap client in java:

Community
  • 1
  • 1
Bass Jobsen
  • 48,736
  • 16
  • 143
  • 224
  • unfortunately, i wans't able to find out what do to with the `"uri" => "urn:String",` and the username and password :( – Daniel S Sep 19 '13 at 17:18
0

I guess you are looking for a Java based soap client for PHP soap service. I had the similar requirement some time back and could find below nice tutorial for the same: http://development.nedeco.de/blog/2011/08/03/java-client-php-soapserver/

Also see this free handy book http://www.ksi.edu/thesis/rhuang/rhuang.pdf

Gyanendra Dwivedi
  • 5,511
  • 2
  • 27
  • 53
0

Groovy is superset of Java, so posting an awesome library you can use in groovy and probably do it in exactly same lines of code as php. https://github.com/jwagenleitner/groovy-wslite

Abe
  • 8,623
  • 10
  • 50
  • 74