java調用php的webService

來源:互聯網
上載者:User

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';所以本人用這樣的編碼試了一下不會產生中文亂碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.