zoukankan      html  css  js  c++  java
  • xmlhttp对象调用webservice要点补疑

             使用xmlhttp对象调用非本地webservice经常也遇到调用失败的情况,这应该是一个不断总结的过程.现列举我所经历过的困扰多时的细节错误,
     假定一个天气预报的服务如下:
             
    using System;
        
    using System.Web;
        
    using System.Web.Services;
        
    using System.Web.Services.Protocols;

        [WebService(Namespace 
    = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo 
    = WsiProfiles.BasicProfile1_1)]
        
    public class Weather : System.Web.Services.WebService
        
    {
                
    public Weather () {

                   
    //Uncomment the following line if using designed components 
                   
    //InitializeComponent(); 
              }


               [WebMethod]
                
    public string GetWeather(string cityName) {
                        
    //根据城市名来返回天气情况
                return "Sunny";
                }
        
        }

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

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

             (2)SOAP 1.2
             

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

             (3)HttpPost 
             

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

           
             (4)HttpGet
             

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


     

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

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


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

  • 相关阅读:
    WebGIS前端地图显示之根据地理范围换算出瓦片行列号的原理(核心) 【系列1-1】
    WMTS服务
    WebGIS 分辨率 比例尺和切片
    Mysql Spatial 空间查询参考
    webapi和传统mvc的context和request的区别
    IIS启动32位运行库
    GEOJSON标准格式学习
    gdal笔记之获取矢量面边界点坐标
    关于TensorFlow,你应该了解的9件事
    C#根据时间范围获取每年每月每周的分组
  • 原文地址:https://www.cnblogs.com/BeanHsiang/p/743673.html
Copyright © 2011-2022 走看看