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';因此本人用這樣的編碼試了一下不會產生中文亂碼,其緣由還不瞭解,望知道者告知 這個例子也只是對webservice的初步瞭解,寫的不足之處望你們給予建議
相關文章
相關標籤/搜索