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 类达到相同的结果?
更新 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。要编译 use javac com/mkyong/client/HelloWorldClient.java
并运行 use java com/mkyong/client/HelloWorldClient
,另请参阅:使用 javac 在一个包中编译四个 java 文件并在命令行中制作 java 包
"映射"到您的 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);
}
除了答案:工作 Soap 客户端示例,您还可以找到许多教程,这些教程告诉您如何在 Java 中编写 SOAP 客户端:
http://www.ibm.com/developerworks/library/x-soapcl/listing1.html
http://docs.oracle.com/cd/E19340-01/820-6767/aeqgc/index.html等。
我猜你正在寻找一个基于 Java 的 PHP soap 服务的 soap 客户端。前段时间我有类似的要求,可以在下面找到相同的好教程:http://development.nedeco.de/blog/2011/08/03/java-client-php-soapserver/
另请参阅这本免费的方便书 http://www.ksi.edu/thesis/rhuang/rhuang.pdf
Groovy是Java的超集,所以发布一个很棒的库,你可以在Groovy中使用,并且可能用与php完全相同的代码行来做 https://github.com/jwagenleitner/groovy-wslite。