<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} -->
转载 java调用php的webService 收藏
1.首先先下载php的webservice包:NuSOAP;
基于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);
}
}