Java 调用PHP的Web Service(三)
usoap是PHP环境中的开源soap工具,算是用得比较多的一个工具了。
在utf-8环境中,nusoap可以工作得很好。但是当用于中文环境中时,nusoap经常会出现一些让人不得其解的问题。
最近一个项目中,服务端是用nusoap实现的,支持UTF-8和GBK两种字符集。
当客户端用GBK来调用服务时,出现错误:Charset from HTTP Content-Type US-ASCII does not match encoding from XML declaration GBK,意思是说,客户端的请求中,HTTP Content-Type的字符集是US-ASCII,而soap请求的XML声明里,字符集是GBK,两者不匹配。检查soap client的request变量,HTTP Content-Type的值也是GBK,怎么会变成了US-ASCII呢?有点莫名其妙了。于是只好跟踪nusoap的源码,发现nusoap在处理HTTP Content-Type时把US-ASCII,ISO-8859-1,UTF-8以外的字符集都默认为US-ASCII。最终发现其原因是因为nusoap使用了xml parser,而xml parser只支持这几种字符集。所以客户端在调用时,当采用GBK编时,调用的HTTP Content-Type 和 soap request的字符集都应该换成ISO-8859-1。
稍后在封装客户端时,也遇到一个类似的问题。客户端字符集声明为GBK,服务端在返回SOAP调用结果时 HTTP Content-Type和soap request都声明字符集为GBK,客户端没有获取任何值。查看soap client的response对象,发现服务端返回正确。为解决这个问题,只好修改服务端,把HTTP Content-Type和soap response的字符集都声明为ISO-8859-1。
所以在使用nusoap时,当遇到GBK或GB2312字符集时,可以使用ISO-8859-1代替。
=============================================================================================
PHP Web Service Server端:
- <?php
- //header("Content-Type:text/html;charset=UTF-8");
- // Pull in the NuSOAP code
- require_once('./lib/nusoap.php');
- // Define the method as a PHP function
- function hello($name) {
- return '你好! ' . $name;
- }
- // Create the server instance
- $server = new soap_server;
- $server->configureWSDL('hellowsdl', 'urn:hellowsdl');
- $server->wsdl->schemaTargetNamespace = 'urn:hellowsdl';
- // Register the method to expose
- $server->register('hello',
- array('name'=>'xsd:string'),
- array('return'=>'xsd:string'),
- 'urn:hellowsdl',
- 'urn:hellowsdl#hello',
- 'rpc',
- 'encoded',
- 'Say hello to somebody'
- );
- // Use the request to (try to) invoke the service
- $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
- $server->service($HTTP_RAW_POST_DATA);
- ?>
Client 端:
- <?php
- //header("Content-Type:text/html;charset=GB2312");
- // Pull in the NuSOAP code
- require_once('./lib/nusoap.php');
- // Create the client instance
- $client = new soapclient('http://localhost/soapTest/helloService.php?wsdl',true);
- // Call the SOAP method
- $param = array("name"=>"安迪");
- $result = $client->call('hello', $param);
- // Display the result
- //print_r($result);
- if(!$err=$client->getError()){
- print_r($result );
- print('</br>');
- echo "程序返回: ", htmlentities($result,ENT_QUOTES,GB2312);
- }
- else{
- echo "错误: ", htmlentities($result,ENT_QUOTES,GB2312);
- }
- echo ' <h2> Request </h2> <pre> ' . htmlspecialchars($client-> request, ENT_QUOTES,GB2312) . ' </pre> ';
- echo ' <h2> Response </h2> <pre> ' . htmlspecialchars($client-> response, ENT_QUOTES,GB2312) . ' </pre> ';
- echo ' <h2> Debug </h2> <pre> ' . htmlspecialchars($client-> debug_str, ENT_QUOTES,GB2312) . ' </pre> ';
- ?>
Java代码:
注意: 要使用Axis1.x, 去官网不要下载了Axis2。好像Axis1.x 和 Axis2还是差别很大的,而且目前Axis1.x的文档比较全点。这些是网上搜到的说法。
如果需要使用中文参数调用Web Service,必须使用ISO-8859-1编码参数,返回的Response再解码。不要使用别的编码,会出错!
java代码
java代码
- import org.apache.axis.client.Service;
- <span style="color: #464646; font-family: simsun; line-height: 21px; text-align: left; white-space: normal; background-color: #ffffff;">import org.apache.axis.client.Call;</span>
- public class WebServiceTest {
- public static void main(String[] args) {
- String endpoint = "http://localhost/soapTest/helloService.php";
- //String endpoint = "http://testweb.dev.php/testWebService/testWebService.php";//该段就是上面刚将的地址
- Service service = new Service();
- Call call;
- try {
- call = (Call) service.createCall();
- call.setTargetEndpointAddress(new java.net.URL(endpoint));
- call.setOperationName("hello");
- String param = new String("安迪".getBytes(),"ISO-8859-1");//如果没有加这段,中文参数将会乱码
- //String param = new String("中文");
- String s = (String) call.invoke(new Object[] {param});
- s = new String(s.getBytes("ISO-8859-1"));//如果没有转换编码,中文也会乱码
- System.out.println(s);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Java 调用PHP的Web Service(三)的更多相关文章
- Java调用.NET 的Web Service服务故障排除
参考路径:http://blog.sina.com.cn/s/blog_4c925dca01014y3r.html
- CentOS 调用.Net 的Web Service,提示连接超时解决方案
我是使用axis调用.NET 的Web Service ,在Window下跑没有问题,将项目部署到Linux下,发现Web Service 连接超时,百度了下,发现是因为Linux不能直接跑.Net, ...
- JAVA 用axis1 调用.NET的web service
1.去官网下载axis的jar包,我下的是1.4版本的 http://axis.apache.org/axis/java/releases.html 2.JAVA 代码: public void my ...
- (转)接口自动化测试 – Java+TestNG 测试 Restful Web Service
本文主要介绍如何用Java针对Restful web service 做接口自动化测试(数据驱动),相比UI自动化,接口自动化稳定性可靠性高,实施难易程度低,做自动化性价比高.所用到的工具或类库有 T ...
- Android调用Asp.net Web Service示例
WebService代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; u ...
- 关于php调用.net的web service 踩过的坑
从前一阵开始,公司要和对方做web service对接.由于对方使用.net语言,而我方使用php.本来经理是要求我们也用.net写web service的服务端.而我上学时学的.net全忘了... ...
- 调用免费的web service(天气预报)
”免费WebService”, 找到提供天气预报Webservice的网络地址 http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl 在url ...
- java 使用AXIS调用远程的web service
1.服务 2.代码 import javax.xml.namespace.QName; import org.apache.axis.client.Call; import org.apache.ax ...
- .Net 调用中国气象台Web Service
接口地址http://www.webxml.com.cn/WebServices/WeatherWebService.asmx 调用步骤:项目添加服务引用-高级-添加web引用 简单代码: web服务 ...
随机推荐
- java 颁发公钥 私钥 php js RSA 加密解密整合
PHP rsa密钥生成 加密解密 - PHP开发 - CSDN博客 https://blog.csdn.net/duzhenxun/article/details/8879227 <?php c ...
- throw and throws in Java
throw and throws in Java - GeeksforGeeks https://www.geeksforgeeks.org/throw-throws-java/ throw and ...
- talib 中文文档(十一):Cycle Indicator Functions 周期指标
Cycle Indicator Functions 不是很懂,欢迎指教 HT_DCPERIOD - Hilbert Transform - Dominant Cycle Period 函数名:HT_D ...
- (2.8)Mysql之SQL基础——索引的分类与使用
(2.8)Mysql之SQL基础——索引的分类与使用 关键字:mysql索引,mysql增加索引,mysql修改索引,mysql删除索引 按逻辑分类: 1.主键索引(聚集索引)(也是唯一索引,不允许有 ...
- mysql 数据操作 单表查询 limit 限制查询的记录数
mysql; +----+-----------+------+-----+------------+---------+--------------+------------+--------+-- ...
- redhat 5的中文包安装
中文包文件名.在iso文件的/server/文件夹下fonts-chinese-3.02-9.6.el5.noarch.rpmfonts-ISO8859-2-75dpi-1.0-17.1.noarch ...
- SqlServer2005 各版本区别
SQL2005 分五个版本,如下所列, 1.Enterprise(企业版), 2.Development(开发版), 3.Workgroup,(工作群版) 4.Standard,(标准版) 5.Exp ...
- linux wa%过高,iostat查看io状况
命令总结: 1. top/vmstat 发现 wa%过高,vmstat b >1: 参考文章: 1. 关于Linux系统指令 top 之 %wa 占用高,用`iostat`探个究竟 最近测试一项 ...
- DB开发之postgresql
1.环境变量配置: PGLIB=/usr/local/pgsql/lib PGDATA=$HOME/data PATH=$PATH:/usr/local/pgsql/bin MANPATH=$MANP ...
- smarty简单语法
什么是smarty及其安装 Smarty是一个php模板引擎,它分开了逻辑程序和外在的内容,提供了一种易于管理的方法. Smarty要求web服务器运行php4.0.6和以上版本. smarty安装需 ...