2

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 类实现相同的结果?

4

3 回答 3

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。要编译 usejavac com/mkyong/client/HelloWorldClient.java并运行 use java com/mkyong/client/HelloWorldClient,另请参阅:Compiling Four java files within one package using javac and making a java package in the command line

“映射”到您的php 示例http://localhost:9999/ws/hello?wsdl将等同于.http://hostname:port/executeCommandhello.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 客户端:

于 2013-09-11T18:12:31.950 回答
0

Groovy 是 Java 的超集,因此发布了一个很棒的库,您可以在 groovy 中使用,并且可能使用与 php 完全相同的代码行来实现。https://github.com/jwagenleitner/groovy-wslite

于 2013-09-16T19:08:38.893 回答
0

我猜您正在为 PHP 肥皂服务寻找基于 Java 的肥皂客户端。一段时间前我有类似的要求,可以在下面找到同样的好教程:http: //development.nedeco.de/blog/2011/08/03/java-client-php-soapserver/

另请参阅这本免费的方便书http://www.ksi.edu/thesis/rhuang/rhuang.pdf

于 2013-09-11T18:29:49.600 回答