这相当于java中这个简短的PHP代码?
$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;
}
是否可以使用简短的java类实现相同的结果?
答案 0 :(得分:1)
更新2 我也许不明白你的问题。 (你能提供你的服务的wdsl吗?)创建像你的PHP代码一样的客户端:
使用:
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"));
}
}
将此文件复制到com / mkyong / client。要编译使用javac com/mkyong/client/HelloWorldClient.java
并运行使用java com/mkyong/client/HelloWorldClient
,请参阅:Compiling four java files within one package using javac和making a java package in the command line
“映射”到您的php示例http://localhost:9999/ws/hello?wsdl
将相当于http://hostname:port/
,executeCommand
将与hello.getHelloWorldAsString
相同。
更新尝试JAX-WS(http://jax-ws.java.net/)
来自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);
}
除了这里的答案:Working Soap client example你可以找到许多教程,告诉你如何在java中编写一个soap客户端:
答案 1 :(得分:0)
我猜你正在为PHP soap服务寻找一个基于Java的soap客户端。 一段时间后我有类似的要求,可以在下面找到相同的教程: http://development.nedeco.de/blog/2011/08/03/java-client-php-soapserver/
答案 2 :(得分:0)
Groovy是Java的超集,所以发布了一个很棒的库,你可以在groovy中使用它,并且可能在与php完全相同的代码行中完成它。 https://github.com/jwagenleitner/groovy-wslite