zoukankan      html  css  js  c++  java
  • 转 java调用php的webService

    1.首先先下载php的webservice包:NuSOAP,自己到官网去下载,链接就不给出来了,自己去google吧
        基于NoSOAP我们写了一个php的webservice的服务端,例子如下:

    <?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的程序了
    例子如下:

    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';所以本人用这样的编码试了一下不会产生中文乱码

  • 相关阅读:
    Hdu 1429 胜利大逃亡(续) (bfs+状态压缩)
    Vijos 1456 最小总代价 (状压dp)
    洛谷 P1313 计算系数 (二项式定理)
    洛谷 P1134 阶乘问题
    EINTR错误
    TCP和UDP协议的应用/参数查看
    BAT面经
    高级环境编程要看的
    UDP丢包和无序 问题的解决方法
    tcp/ip
  • 原文地址:https://www.cnblogs.com/Holmes-Jin/p/3611138.html
Copyright © 2011-2022 走看看