前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java调用php的webService

java调用php的webService

作者头像
黄啊码
发布2020-05-29 10:29:51
1.7K0
发布2020-05-29 10:29:51
举报

虽然个人也写过一个,但年代久远,拿一个现成的解决方案供大家参考

1.首先先下载php的webservice包:NuSOAP,自己到官网去下载,链接就不给出来了,自己去google吧

基于NoSOAP我们写了一个php的webservice的服务端,例子如下:

代码语言:javascript
复制

 <?php
     header("Content-Type:text/html;charset=UTF-8");
     require('../lib/nusoap.php');
 
     $server = new soap_server();
     $server->configureWSDL('hellowsdl', 'urn:hellowsdl');
     $server->wsdl->schemaTargetNamespace = 'urn:hellowsdl';
 
     $server->register('hello',                 // method name
     array('name' => 'xsd:string'),         // input parameters
     array('return' => 'xsd:string'),       // output parameters
     'urn:hellowsdl',                       // namespace
     'urn:hellowsdl#hello',                 // soapaction
     'rpc',                                 // style
     'encoded',                             // use
     'Says hello to the caller'             // documentation
     );
 
     function hello($name) {
         if($name == ''){
             return new soap_fault('Client','','Must supply a valid name.');
         }
         return 'Hello 你好!:, ' . $name;
     }
 
     $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
 
     $server->service($HTTP_RAW_POST_DATA);
 ?>

写完服务端后,自己得先测试一下,访问一下该php页面就可以看到如下的页面: 点击WSDL后,将可以看到wsdl定义的xml报文,把<soap:address location="[url]http://testweb.dev.php/testWebService/testWebService.php[/url]" /> 这串里面的[url]http://testweb.dev.php/testWebService/testWebService.php[/url]拷贝到java程序,下面的java调用webservice将会用到 现在开始写java调用webservice的程序了 例子如下:

代码语言:javascript
复制

 package test;

 import javax.xml.rpc.ServiceException;

 import org.apache.axis.client.Call;
 import org.apache.axis.client.Service;

 public class TestWebService {

     public static void main(String[] args) throws Exception {
         //String endpoint = "http://localhost:8086/testwebservice/services/TestWebService";
         String endpoint = "http://testweb.dev.php/testWebService/testWebService.php";//该段就是上面刚将的地址 
         Service service = new Service();
         Call call = (Call) service.createCall();
         call.setTargetEndpointAddress(new java.net.URL(endpoint));
         call.setOperationName("hello");

         String param = new String("中文".getBytes(),"ISO-8859-1");//如果没有加这段,中文参数将会乱码
         String s = (String) call.invoke(new Object[] {param});
         s = new String(s.getBytes("ISO-8859-1"),"GBK");//如果没有转换编码,中文也会乱码
         System.out.println(s);
     }
 
 }

nusoap.php定义这样的编码var $soap_defencoding = 'ISO-8859-1';所以本人用这样的编码试了一下不会产生中文乱码

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-12-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档