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端:

  1. <?php
  2. //header("Content-Type:text/html;charset=UTF-8");
  3. // Pull in the NuSOAP code
  4. require_once('./lib/nusoap.php');
  5. // Define the method as a PHP function
  6. function hello($name) {
  7. return '你好! ' . $name;
  8. }
  9. // Create the server instance
  10. $server = new soap_server;
  11. $server->configureWSDL('hellowsdl', 'urn:hellowsdl');
  12. $server->wsdl->schemaTargetNamespace = 'urn:hellowsdl';
  13. // Register the method to expose
  14. $server->register('hello',
  15. array('name'=>'xsd:string'),
  16. array('return'=>'xsd:string'),
  17. 'urn:hellowsdl',
  18. 'urn:hellowsdl#hello',
  19. 'rpc',
  20. 'encoded',
  21. 'Say hello to somebody'
  22. );
  23. // Use the request to (try to) invoke the service
  24. $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
  25. $server->service($HTTP_RAW_POST_DATA);
  26. ?>

   Client 端:

  1. <?php
  2. //header("Content-Type:text/html;charset=GB2312");
  3. // Pull in the NuSOAP code
  4. require_once('./lib/nusoap.php');
  5. // Create the client instance
  6. $client = new soapclient('http://localhost/soapTest/helloService.php?wsdl',true);
  7. // Call the SOAP method
  8. $param = array("name"=>"安迪");
  9. $result = $client->call('hello', $param);
  10. // Display the result
  11. //print_r($result);
  12. if(!$err=$client->getError()){
  13. print_r($result );
  14. print('</br>');
  15. echo "程序返回: ", htmlentities($result,ENT_QUOTES,GB2312);
  16. }
  17. else{
  18. echo "错误: ", htmlentities($result,ENT_QUOTES,GB2312);
  19. }
  20. echo   ' <h2> Request </h2> <pre> '   .   htmlspecialchars($client-> request,   ENT_QUOTES,GB2312)   .   ' </pre> ';
  21. echo   ' <h2> Response </h2> <pre> '   .   htmlspecialchars($client-> response,   ENT_QUOTES,GB2312)   .   ' </pre> ';
  22. echo   ' <h2> Debug </h2> <pre> '   .   htmlspecialchars($client-> debug_str,   ENT_QUOTES,GB2312)   .   ' </pre> ';
  23. ?>

  Java代码:

注意: 要使用Axis1.x, 去官网不要下载了Axis2。好像Axis1.x 和 Axis2还是差别很大的,而且目前Axis1.x的文档比较全点。这些是网上搜到的说法。

如果需要使用中文参数调用Web Service,必须使用ISO-8859-1编码参数,返回的Response再解码。不要使用别的编码,会出错!

java代码

       ...

java代码

  1. import org.apache.axis.client.Service;
  2. <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>
  3. public class WebServiceTest {
  4. public static void main(String[] args) {
  5. String endpoint = "http://localhost/soapTest/helloService.php";
  6. //String endpoint = "http://testweb.dev.php/testWebService/testWebService.php";//该段就是上面刚将的地址
  7. Service service = new Service();
  8. Call call;
  9. try {
  10. call = (Call) service.createCall();
  11. call.setTargetEndpointAddress(new java.net.URL(endpoint));
  12. call.setOperationName("hello");
  13. String param = new String("安迪".getBytes(),"ISO-8859-1");//如果没有加这段,中文参数将会乱码
  14. //String param = new String("中文");
  15. String s = (String) call.invoke(new Object[] {param});
  16. s = new String(s.getBytes("ISO-8859-1"));//如果没有转换编码,中文也会乱码
  17. System.out.println(s);
  18. } catch (Exception e) {
  19. // TODO Auto-generated catch block
  20. e.printStackTrace();
  21. }
  22. }
  23. }

Java 调用PHP的Web Service(三)的更多相关文章

  1. Java调用.NET 的Web Service服务故障排除

    参考路径:http://blog.sina.com.cn/s/blog_4c925dca01014y3r.html

  2. CentOS 调用.Net 的Web Service,提示连接超时解决方案

    我是使用axis调用.NET 的Web Service ,在Window下跑没有问题,将项目部署到Linux下,发现Web Service 连接超时,百度了下,发现是因为Linux不能直接跑.Net, ...

  3. JAVA 用axis1 调用.NET的web service

    1.去官网下载axis的jar包,我下的是1.4版本的 http://axis.apache.org/axis/java/releases.html 2.JAVA 代码: public void my ...

  4. (转)接口自动化测试 – Java+TestNG 测试 Restful Web Service

    本文主要介绍如何用Java针对Restful web service 做接口自动化测试(数据驱动),相比UI自动化,接口自动化稳定性可靠性高,实施难易程度低,做自动化性价比高.所用到的工具或类库有 T ...

  5. Android调用Asp.net Web Service示例

    WebService代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; u ...

  6. 关于php调用.net的web service 踩过的坑

    从前一阵开始,公司要和对方做web service对接.由于对方使用.net语言,而我方使用php.本来经理是要求我们也用.net写web service的服务端.而我上学时学的.net全忘了... ...

  7. 调用免费的web service(天气预报)

    ”免费WebService”, 找到提供天气预报Webservice的网络地址 http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl 在url ...

  8. java 使用AXIS调用远程的web service

    1.服务 2.代码 import javax.xml.namespace.QName; import org.apache.axis.client.Call; import org.apache.ax ...

  9. .Net 调用中国气象台Web Service

    接口地址http://www.webxml.com.cn/WebServices/WeatherWebService.asmx 调用步骤:项目添加服务引用-高级-添加web引用 简单代码: web服务 ...

随机推荐

  1. htop 分析 进程对资源的消耗

    [root@d ~]# htop -hhtop 2.2.0 - (C) 2004-2018 Hisham MuhammadReleased under the GNU GPL. -C --no-col ...

  2. ubuntu low graphic mode---disable docker -self start.

    --------- /etc/default/docker.conf-----设置启动参数. /etc/init/docker.conf------------不好使(only mysql) star ...

  3. 整合最优雅SSM框架:SpringMVC + Spring + MyBatis 基础

    在写代码之前我们先了解一下这三个框架分别是干什么的? 相信大以前也看过不少这些概念,我这就用大白话来讲,如果之前有了解过可以跳过这一大段,直接看代码! SpringMVC:它用于web层,相当于con ...

  4. python拼接字符串

    python拼接字符串一般有以下几种方法: 1.直接通过(+)操作符拼接 s = 'Hello' + ' ' + 'World' + '!' print(s) 输出结果:Hello World! 使用 ...

  5. Yarn之ResourceManager详细分析

    一.概述     本文将介绍ResourceManager在Yarn中的功能作用,从更细的粒度分析RM内部组成的各个组件功能和他们相互的交互方式. 二.ResourceManager的交互协议与基本职 ...

  6. stringbuffer 和 stringbuilder区别

    stringbuffer  和  stringbuilder速度                 小于         线程安全           线程非安全 单线程操作大量数据用stringbui ...

  7. CCPC-Wannafly Winter Camp Day4 (Div2, onsite)

    Replay Dup4: 两轮怎么退火啊? 简单树形dp都不会了,送了那么多罚时 简单题都不想清楚就乱写了,喵喵喵? X: 欧拉怎么回路啊, 不会啊. 还是有没有手误?未思考清楚或者未检查就提交, 导 ...

  8. zw版【转发·台湾nvp系列Delphi例程】HALCON DispArc

    zw版[转发·台湾nvp系列Delphi例程]HALCON DispArc zw版[转发·台湾nvp系列Delphi例程]HALCON DispArc----------RAD Studio XE D ...

  9. SQL :模糊查询,转义字符

    1. 查询table表name列包含 '_BCE' 的记录 select * from table where name like '_BCE%' ABCEDF _BCEFG _BCEDF 3 row ...

  10. react headtop title 截取

    render() { const nav = this.props.nav const text = nav && nav.length > 5 ? this.strHandle ...