xmlhttp对象调用webservice要点补疑

         使用xmlhttp对象调用非本地webservice经常也遇到调用失败的情况,这应该是一个不断总结的过程.现列举我所经历过的困扰多时的细节错误,
 假定一个天气预报的服务如下:
         
None.gifusing System;
None.gif    
using System.Web;
None.gif    
using System.Web.Services;
None.gif    
using System.Web.Services.Protocols;
None.gif
None.gif    [WebService(Namespace 
= "http://tempuri.org/")]
None.gif    [WebServiceBinding(ConformsTo 
= WsiProfiles.BasicProfile1_1)]
None.gif    
public class Weather : System.Web.Services.WebService
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
public Weather () dot.gif{
InBlock.gif
InBlock.gif               
//Uncomment the following line if using designed components 
InBlock.gif               
//InitializeComponent(); 
ExpandedSubBlockEnd.gif
          }

InBlock.gif
InBlock.gif           [WebMethod]
ExpandedSubBlockStart.gifContractedSubBlock.gif            
public string GetWeather(string cityName) dot.gif{
InBlock.gif                    
//根据城市名来返回天气情况
InBlock.gif
            return "Sunny";
ExpandedSubBlockEnd.gif            }
    
ExpandedBlockEnd.gif    }

None.gif

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

None.gifvar data ='<?xml version="1.0" encoding="utf-8"?>'
None.gif    
+'<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/">'
None.gif    
+'<soap:Body>'
None.gif    
+'<getWeather xmlns="http://tempuri.org/">'
None.gif    
+'<cityName>hangzhou</cityName>'   
None.gif        
+'</getWeather>'
None.gif    
+'</soap:Body>'
None.gif    
+'</soap:Envelope>'
ExpandedBlockStart.gifContractedBlock.gif     
function RequestService()dot.gif
InBlock.gif        
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
InBlock.gif        
//Webservice location.
InBlock.gif
        var URL="http://localhost:1323/WebSite/weather.asmx";
InBlock.gif        xmlhttp.Open(
"Post",URL, false); 
InBlock.gif        xmlhttp.SetRequestHeader (
"Content-Type","text/xml; charset=utf-8"); 
InBlock.gif        xmlhttp.SetRequestHeader (
"Content-Length",data.length); 
InBlock.gif        xmlhttp.SetRequestHeader (
"SOAPAction","http://tempuri.org/GetWeather"); 
InBlock.gif        xmlhttp.Send(data);         
ExpandedBlockEnd.gif    }

None.gif

         (2)SOAP 1.2
         

None.gifvar data = '<?xml version="1.0" encoding="utf-8"?>'
None.gif        
+'<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
None.gif        
+'xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">'
None.gif        
+'<soap12:Body>'
None.gif        
+'<getWeather xmlns="http://tempuri.org/">'
None.gif        
+'<cityName>hangzhou</cityName>'
None.gif        
+'</getWeather>'
None.gif        
+'</soap12:Body>'
None.gif        
+'</soap12:Envelope>'
ExpandedBlockStart.gifContractedBlock.gif     
function RequestService()dot.gif
InBlock.gif        
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
InBlock.gif        
//Webservice location.
InBlock.gif
        var URL="http://localhost:1323/WebSite/weather.asmx";
InBlock.gif        xmlhttp.Open(
"Post",URL, false); 
InBlock.gif        xmlhttp.SetRequestHeader (
"Content-Type"," application/soap+xml; charset=utf-8"); 
InBlock.gif        xmlhttp.SetRequestHeader (
"Content-Length",data.length);         
InBlock.gif        xmlhttp.Send(data);         
ExpandedBlockEnd.gif    }

None.gif

         (3)HttpPost 
         

None.gifvar data = 'cityName=hangzhou'
ExpandedBlockStart.gifContractedBlock.gif     
function RequestService()dot.gif
InBlock.gif        
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
InBlock.gif        
//Webservice location.
InBlock.gif
        var URL="http://localhost:1323/WebSite/weather.asmx/getWeather";
InBlock.gif        xmlhttp.Open(
"Post",URL, false); 
InBlock.gif        xmlhttp.SetRequestHeader (
"Content-Type","  application/x-www-form-urlencoded"); 
InBlock.gif        xmlhttp.SetRequestHeader (
"Content-Length",data.length);         
InBlock.gif        xmlhttp.Send(data);         
ExpandedBlockEnd.gif    }

None.gif

       
         (4)HttpGet
         

ExpandedBlockStart.gifContractedBlock.giffunction RequestService()dot.gif
InBlock.gif        
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
InBlock.gif        
//Webservice location.
InBlock.gif
        var URL="http://localhost:1323/WebSite/weather.asmx/getWeather?cityName=hangzhou";
InBlock.gif        xmlhttp.Open(
"GET ",URL, false); 
InBlock.gif        xmlhttp.Send(
null);
ExpandedBlockEnd.gif    }

None.gif


 

         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补上如下配置
         

None.gif    <webServices>
None.gif 
<conformanceWarnings>
None.gif    
<clear />
None.gif    
<add name="BasicProfile1_1" />
None.gif  
</conformanceWarnings>
None.gif  
<protocols>
None.gif    
<add name="HttpSoap1.2"/>
None.gif    
<add name="HttpSoap"/>
None.gif    
<add name="HttpPostLocalhost"/>
None.gif    
<add name="HttpPost"/> 
None.gif    
<add name="HttpGet"/> 
None.gif    
<add name="Documentation"/>
None.gif  
</protocols>
None.gif  
<soapExtensionTypes>
None.gif  
</soapExtensionTypes>
None.gif  
<soapExtensionReflectorTypes>
None.gif  
</soapExtensionReflectorTypes>
None.gif  
<soapExtensionImporterTypes>
None.gif  
</soapExtensionImporterTypes>
None.gif  
<wsdlHelpGenerator href="DefaultWsdlHelpGenerator.aspx"/>
None.gif  
<serviceDescriptionFormatExtensionTypes>
None.gif  
</serviceDescriptionFormatExtensionTypes>
None.gif
</webServices>


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

转载于:https://www.cnblogs.com/BeanHsiang/archive/2007/05/12/743673.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值