使用xmlhttp对象调用非本地webservice经常也遇到调用失败的情况,这应该是一个不断总结的过程.现列举我所经历过的困扰多时的细节错误,
 假定一个天气预报的服务如下:
         
xmlhttp对象调用webservice要点补疑using System;
xmlhttp对象调用webservice要点补疑    
using System.Web;
xmlhttp对象调用webservice要点补疑    
using System.Web.Services;
xmlhttp对象调用webservice要点补疑    
using System.Web.Services.Protocols;
xmlhttp对象调用webservice要点补疑
xmlhttp对象调用webservice要点补疑    [WebService(Namespace 
= "http://tempuri.org/")]
xmlhttp对象调用webservice要点补疑    [WebServiceBinding(ConformsTo 
= WsiProfiles.BasicProfile1_1)]
xmlhttp对象调用webservice要点补疑    
public class Weather : System.Web.Services.WebService

         1. 请求的方法 根据协议有Soap 1.1 Soap1.2 HttpPost HttpGet 四种方式
         
         (1)SOAP 1.1
         

xmlhttp对象调用webservice要点补疑var data ='<?xml version="1.0" encoding="utf-8"?>'
xmlhttp对象调用webservice要点补疑    
+'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '    +'xmlns:xsd="http://www.w3.org/2001/XMLSchema"'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'
xmlhttp对象调用webservice要点补疑    
+'<soap:Body>'
xmlhttp对象调用webservice要点补疑    
+'<getWeather xmlns="http://tempuri.org/">'
xmlhttp对象调用webservice要点补疑    
+'<cityName>hangzhou</cityName>'   
xmlhttp对象调用webservice要点补疑        
+'</getWeather>'
xmlhttp对象调用webservice要点补疑    
+'</soap:Body>'
xmlhttp对象调用webservice要点补疑    
+'</soap:Envelope>'

         (2)SOAP 1.2
         

xmlhttp对象调用webservice要点补疑var data = '<?xml version="1.0" encoding="utf-8"?>'
xmlhttp对象调用webservice要点补疑        
+'<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
xmlhttp对象调用webservice要点补疑        
+'xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">'
xmlhttp对象调用webservice要点补疑        
+'<soap12:Body>'
xmlhttp对象调用webservice要点补疑        
+'<getWeather xmlns="http://tempuri.org/">'
xmlhttp对象调用webservice要点补疑        
+'<cityName>hangzhou</cityName>'
xmlhttp对象调用webservice要点补疑        
+'</getWeather>'
xmlhttp对象调用webservice要点补疑        
+'</soap12:Body>'
xmlhttp对象调用webservice要点补疑        
+'</soap12:Envelope>'

         (3)HttpPost 
         

xmlhttp对象调用webservice要点补疑var data = 'cityName=hangzhou'

       
         (4)HttpGet
         


 

         2.中文乱码
         webservice接收和传输的消息都是UTF-8编码的,网上有很多解决GB2312传中文参数和接收中文消息乱码的办法,比如用prototype扩展xmlhttp对象,其实也不用那么麻烦,传递参数用escape转换一次,接收时用UTF8Encoding读取出来再根据需要转换即可,解决办法原理差不多.
 
         3.解析返回的Xml DomDocument对象无法使用selectNodes和selectSingleNode
          其中一个原因是mozilla不支持此方法,可以通过prototype为它的domdocument对象扩展这两个同名方法,便于用相同的方法处理MS和mozilla兼容问题。但有时就是在IE里也不能正常使用这两个方法,常见情况是取出来的节点值为空。解决办法是在生成的domdocument对象(假设对象为xmlDom)设置一个属性 xmlDom.setProerty("Namespace",'xmlns="na:http://tempuri.org/"')指定一个默认命名空间的前缀,然后在selectNodes和selectSingleNode中使用前缀加标签名如 na:weather就可以了。

          4.“意外的以servicemethod/结尾“的错误(类似的格式)
          客户端以HttpGet或HttpPost方式调用非本地webservice报“意外的以servicemethod/结尾“的错误,其中一个原因是部署webservice的站点配置中默认是不开启httpget和httppost非本域请求许可的,因此要在web.config补上如下配置
         

xmlhttp对象调用webservice要点补疑    <webServices>
xmlhttp对象调用webservice要点补疑 
<conformanceWarnings>
xmlhttp对象调用webservice要点补疑    
<clear />
xmlhttp对象调用webservice要点补疑    
<add name="BasicProfile1_1" />
xmlhttp对象调用webservice要点补疑  
</conformanceWarnings>
xmlhttp对象调用webservice要点补疑  
<protocols>
xmlhttp对象调用webservice要点补疑    
<add name="HttpSoap1.2"/>
xmlhttp对象调用webservice要点补疑    
<add name="HttpSoap"/>
xmlhttp对象调用webservice要点补疑    
<add name="HttpPostLocalhost"/>
xmlhttp对象调用webservice要点补疑    
<add name="HttpPost"/> 
xmlhttp对象调用webservice要点补疑    
<add name="HttpGet"/> 
xmlhttp对象调用webservice要点补疑    
<add name="Documentation"/>
xmlhttp对象调用webservice要点补疑  
</protocols>
xmlhttp对象调用webservice要点补疑  
<soapExtensionTypes>
xmlhttp对象调用webservice要点补疑  
</soapExtensionTypes>
xmlhttp对象调用webservice要点补疑  
<soapExtensionReflectorTypes>
xmlhttp对象调用webservice要点补疑  
</soapExtensionReflectorTypes>
xmlhttp对象调用webservice要点补疑  
<soapExtensionImporterTypes>
xmlhttp对象调用webservice要点补疑  
</soapExtensionImporterTypes>
xmlhttp对象调用webservice要点补疑  
<wsdlHelpGenerator href="DefaultWsdlHelpGenerator.aspx"/>
xmlhttp对象调用webservice要点补疑  
<serviceDescriptionFormatExtensionTypes>
xmlhttp对象调用webservice要点补疑  
</serviceDescriptionFormatExtensionTypes>
xmlhttp对象调用webservice要点补疑
</webServices>


         希望打算使用xmlhttp的朋友能够避免这些小问题,也希望大家能够将自己遇到的问题和解决方法继续补全,方便查找也提高自己。上面列举的问题如有更易行的方法,也请指出来,交流一下。

相关文章: