您的位置:首页 > 编程语言 > PHP开发

java调用php的webService

2008-06-10 15:05 316 查看
    

java调用php的webService

标签:
webservicejavaphpstringsoapparameters

2008-06-10 15:05
3987人阅读 评论(0)
收藏
举报

本文章已收录于:


分类:
技术空间(717)




作者同类文章X

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="http://testweb.dev.php/testWebService/testWebService.php" /> 这串里面的http://testweb.dev.php/testWebService/testWebService.php拷贝到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';所以本人用这样的编码试了一下不会产生中文乱码,其原因还不了解,望知道者告知

这个例子也只是对webservice的初步了解,写的不足之处望大家给予建议



顶0踩0
 
 
上一篇超级炫的AJAX框架Echo2
下一篇XStream 学习手记
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: