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服务 ...
随机推荐
- htop 分析 进程对资源的消耗
[root@d ~]# htop -hhtop 2.2.0 - (C) 2004-2018 Hisham MuhammadReleased under the GNU GPL. -C --no-col ...
- ubuntu low graphic mode---disable docker -self start.
--------- /etc/default/docker.conf-----设置启动参数. /etc/init/docker.conf------------不好使(only mysql) star ...
- 整合最优雅SSM框架:SpringMVC + Spring + MyBatis 基础
在写代码之前我们先了解一下这三个框架分别是干什么的? 相信大以前也看过不少这些概念,我这就用大白话来讲,如果之前有了解过可以跳过这一大段,直接看代码! SpringMVC:它用于web层,相当于con ...
- python拼接字符串
python拼接字符串一般有以下几种方法: 1.直接通过(+)操作符拼接 s = 'Hello' + ' ' + 'World' + '!' print(s) 输出结果:Hello World! 使用 ...
- Yarn之ResourceManager详细分析
一.概述 本文将介绍ResourceManager在Yarn中的功能作用,从更细的粒度分析RM内部组成的各个组件功能和他们相互的交互方式. 二.ResourceManager的交互协议与基本职 ...
- stringbuffer 和 stringbuilder区别
stringbuffer 和 stringbuilder速度 小于 线程安全 线程非安全 单线程操作大量数据用stringbui ...
- CCPC-Wannafly Winter Camp Day4 (Div2, onsite)
Replay Dup4: 两轮怎么退火啊? 简单树形dp都不会了,送了那么多罚时 简单题都不想清楚就乱写了,喵喵喵? X: 欧拉怎么回路啊, 不会啊. 还是有没有手误?未思考清楚或者未检查就提交, 导 ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON DispArc
zw版[转发·台湾nvp系列Delphi例程]HALCON DispArc zw版[转发·台湾nvp系列Delphi例程]HALCON DispArc----------RAD Studio XE D ...
- SQL :模糊查询,转义字符
1. 查询table表name列包含 '_BCE' 的记录 select * from table where name like '_BCE%' ABCEDF _BCEFG _BCEDF 3 row ...
- react headtop title 截取
render() { const nav = this.props.nav const text = nav && nav.length > 5 ? this.strHandle ...