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 =" 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值