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

     

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

     

       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.   
    13. if(!$err=$client->getError()){  
    14.     print_r($result );  
    15.     print('</br>');  
    16.     echo "程序返回: ", htmlentities($result,ENT_QUOTES,GB2312);  
    17. }  
    18. else{  
    19.     echo "错误: ", htmlentities($result,ENT_QUOTES,GB2312);  
    20. }  
    21.   
    22. echo   ' <h2> Request </h2> <pre> '   .   htmlspecialchars($client-> request,   ENT_QUOTES,GB2312)   .   ' </pre> ';   
    23. echo   ' <h2> Response </h2> <pre> '   .   htmlspecialchars($client-> response,   ENT_QUOTES,GB2312)   .   ' </pre> ';   
    24. echo   ' <h2> Debug </h2> <pre> '   .   htmlspecialchars($client-> debug_str,   ENT_QUOTES,GB2312)   .   ' </pre> ';   
    25.   
    26. ?>  

     

     

      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.   
    3. <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>  
    4.   
    5. public class WebServiceTest {  
    6.     public static void main(String[] args) {  
    7.         String endpoint = "http://localhost/soapTest/helloService.php";  
    8.         //String endpoint = "http://testweb.dev.php/testWebService/testWebService.php";//该段就是上面刚将的地址   
    9.         Service service = new Service();  
    10.         Call call;  
    11.         try {  
    12.         call = (Call) service.createCall();  
    13.         call.setTargetEndpointAddress(new java.net.URL(endpoint));  
    14.         call.setOperationName("hello");  
    15.           
    16.   
    17.         String param = new String("安迪".getBytes(),"ISO-8859-1");//如果没有加这段,中文参数将会乱码  
    18.         //String param = new String("中文");  
    19.         String s = (String) call.invoke(new Object[] {param});  
    20.         s = new String(s.getBytes("ISO-8859-1"));//如果没有转换编码,中文也会乱码  
    21.         System.out.println(s);  
    22.         } catch (Exception e) {  
    23.             // TODO Auto-generated catch block  
    24.             e.printStackTrace();  
    25.         }  
    26.     }  
  • 相关阅读:
    如何提升公司自动化测试技术水平
    一位寒门博士的致谢,女友回复...
    胆小鬼博弈:普通人怎么做出最佳选择?
    2021年年末全国男女比例数据公布
    数据测试方法
    如何成为一名拖垮团队的程序员
    负激励
    工作最累的部分
    Linux高级I/O函数 dup, dup2, dup3
    笔记:Vue中防抖(debounce)、节流(throttle)的介绍与运用
  • 原文地址:https://www.cnblogs.com/MaxElephant/p/8177513.html
Copyright © 2020-2023  润新知