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 ?

3
  • Take a look at the resources linked from this answer: 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. Commented Sep 10, 2013 at 10:01
  • duplicate of stackoverflow.com/questions/15948927/… Commented Sep 11, 2013 at 18:01
  • @ Bass Jobsen - i haven't found how to run this executeCommand in link you provided Commented Sep 11, 2013 at 18:29

3 Answers 3

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:

Sign up to request clarification or add additional context in comments.

1 Comment

unfortunately, i wans't able to find out what do to with the "uri" => "urn:String", and the username and password :(
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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.