免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6340 | 回复: 0
打印 上一主题 下一主题

java调用php的webservice简单写法 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-01-18 18:09 |只看该作者 |倒序浏览
关掉isa防火墙

xfire方式

public class XfireClient {
    public static void main(String[] args) throws MalformedURLException,Exception {
  //XFire client
  Client client = new Client(new URL(
          "
http://www.abc.com/server.php?wsdl
"));
  
  //第一个参数是方法名,后面的参数是需要传入的参数
  Object[] results = client.invoke("publish", new Object[]{"localhost","abc"});
  
  System.out.println((String)results[0]);
  
  }
}


axis1.4方式

public class AxisClient {
    private final static String endpoint = "
http://www.abc.com/server.php
";
    public static void main(String[] args) throws Exception {
        Service service = new Service();
        Call call = (Call) service.createCall();
        call.setTargetEndpointAddress(new URL(endpoint));
        call.setOperationName(new QName("urn","publish"));
        call.addParameter("hostname", XMLType.XSD_STRING, ParameterMode.IN);
        call.addParameter("username", XMLType.XSD_STRING, ParameterMode.IN);
        call.setReturnType(XMLType.XSD_STRING);
        String result = (String) call.invoke(new Object[] { "localhost","root });
        System.out.println(result);
    }
}

axis2方式

public class AxisClient {
public static void main(String[] args) throws Exception {
   RPCServiceClient serviceClient = new RPCServiceClient();
         Options options = serviceClient.getOptions();
// 这一步指定了该服务的提供地址
         EndpointReference targetEPR = new EndpointReference(
                 "
http://www.abc.com/server.php
");
// 将option绑定到该服务地址               
         options.setTo(targetEPR);
         // 添加具体要调用的方法,这个可以从该服务的wsdl文件中得知
         // 第一个参数是该服务的targetNamespace,第二个为你所要调用
         // 的operation名称
        
         QName opAdd =
             new QName("urn", "publish");
        
         //设置返回值类型
         Class[] returnTypes = new Class[] {String.class};
         //设置调用的参数
         Object[] opAddArgs = new Object[] {"localhost","root"};
         //调用服务,获得返回值
         Object[] response = serviceClient.invokeBlocking(opAdd, opAddArgs, returnTypes);
        
        
         String res = (String)response[0];
         if (res == null) {
          System.out.println("wrong");
          return;
         }
         System.out.println(res);
}

myeclipse5.5.1生成客户端

http://www.abc.com/server.php?wsdl
需要后面加wsdl,验证警告不必管它,可以成功生成客户端。

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/110188/showart_2152347.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP