当前位置:编程学习 > C#/ASP.NET >>

soap协议调用Webservice与NuSoap java/C# webservice中文乱码问题

方法一:直接调用

 代码如下 复制代码
<?   
  
  
include(‘NuSoap.php’);   
  
// 创建一个soapclient对象,参数是server的WSDL   
$client = new soapclient(‘http://localhost/Webservices/Service.asmx?WSDL’, 'wsdl’);   
  
// 参数转为数组形式传递   
$aryPara = array(‘strUsername’=>’username’, ‘strPassword’=>MD5(‘password’));   
  
// 调用远程函数   
$aryResult = $client->call(‘login’,$aryPara);   
  
//echo $client->debug_str;   
    
  
$document=$client->document;   
echo <<<SoapDocument   
<?xml version=”1.0″ encoding=”GB2312″?>   
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle=”http://schemas.xmlsoap.org/soap/encoding/” xmlns:SOAP-ENV=”http://schemas.xmlsoap.org/soap/envelope/” xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:SOAP-ENC=”http://schemas.xmlsoap.org/soap/encoding/” xmlns:si=”http://soapinterop.org/xsd“>   
<SOAP-ENV:Body>   
$document  
</SOAP-ENV:Body>   
</SOAP-ENV:Envelope>   
SoapDocument;   
  
?>   

方法二:代理方式调用

 

 代码如下 复制代码
<?   
  
  
require(‘NuSoap.php’);   
  
//创建一个soapclient对象,参数是server的WSDL   
$client=new soapclient(‘http://localhost/Webservices/Service.asmx?WSDL’, 'wsdl’);   
  
//生成proxy类   
$proxy=$client->getProxy();   
  
//调用远程函数   
$aryResult=$proxy->login(‘username’,MD5(‘password’));   
  
//echo $client->debug_str;   
    
  
$document=$proxy->document;   
echo <<<SoapDocument   
<?xml version=”1.0″ encoding=”GB2312″?>   
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle=”http://schemas.xmlsoap.org/soap/encoding/” xmlns:SOAP-ENV=”http://schemas.xmlsoap.org/soap/envelope/” xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:SOAP-ENC=”http://schemas.xmlsoap.org/soap/encoding/” xmlns:si=”http://soapinterop.org/xsd“>   
<SOAP-ENV:Body>   
$document  
</SOAP-ENV:Body>   
</SOAP-ENV:Envelope>   
SoapDocument;   
  
?>   


 
NuSoap调用WebService出现乱码的原因:

通常我们进行WebService开发时都是用的UTF-8编码,这时我们需要设置:

 代码如下 复制代码

$client->soap_defencoding = ‘utf-8′;

同时,需要让xml以同样的编码方式传递:

 代码如下 复制代码

$client->xml_encoding = ‘utf-8′;

至此应该是一切正常了才对,但是我们在输出结果的时候,却发现返回的是乱码。

NuSoap调用WebService出现乱码的解决方法:

实际上,开启了调试功能的朋友,相信会发现$client->response返回的是正确的结果,为什么

 代码如下 复制代码

$result = $client->call($action, array(‘parameters’ => $param));

却是乱码呢?


研究过NuSoap代码后我们会发现,当xml_encoding设置为UTF-8时,NuSoap会检测decode_utf8的设置,如果为true,会执行 PHP 里面的utf8_decode函数,而NuSoap默认为true,因此,我们需要设置:


PHP 代码

 代码如下 复制代码
$client->soap_defencoding = ‘utf-8′;   
$client->decode_utf8 = false;   
$client->xml_encoding = ‘utf-8′;   
补充:asp.net教程,WebService 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,