认证标题Titanium Appcelerator的Soap调用失败

时间:2014-11-12 10:26:11

标签: web-services soap titanium

我想在我的钛项目中使用ASMX服务,因为我使用了Kitchen Sink Nook项目,它适用于货币转换器asmx webservice并且它没有认证头。

但是我的客户端webservice有身份验证标头,我在SOAP UI中测试了下面的webservice,它运行得很完美。但我没有得到Titanium SOAP Call的解决方案。

Calling_class.js

Titanium.include('suds.js');
var window = Ti.UI.createWindow({
    title:"Web Service Test",
    backgroundColor:"#FFF",
    exitOnClose:true

});
var label = Ti.UI.createLabel({
    top: 10,
    left: 10,
    width: 'auto',
    height: 'auto',
    text: 'Contacting currency rates web service...'
});

var bouton = Titanium.UI.createButton({

    title:"Test",
    bottom:20,
    height:120,
    width:135,  
});

window.add(label);


var url = "http://192.168.15.45:8082/CrmAppointments.asmx";
var callparams = {
    AppointmentNumber: 'APP-00000003-H042S5',
    UserId: '4'
};

var suds = new SudsClient({
    endpoint: url,
    targetNamespace: 'http://tempuri.org'
});

bouton.addEventListener("click", function(e){

try {
    suds.invoke('BookAppointmentFromWCF', callparams, function(xmlDoc) {
 console.log('response : '+ this.responseText);
        var results = xmlDoc.documentElement.getElementsByTagName('BookAppointmentFromWCF');
        if (results && results.length>0) {
            var result = results.item(0);
            label.text = '1 Euro buys you ' + results.item(0).text + ' U.S. Dollars.';
        } else {
            label.text = 'Oops, could not determine result of SOAP call.';
        }
    });
} catch(e) {
    Ti.API.error('Error: ' + e);
}
});
window.add(bouton);
window.open();

suds.js

/**
* Suds: A Lightweight JavaScript SOAP Client
* Copyright: 2009 Kevin Whinnery (http://www.kevinwhinnery.com)
* License: http://www.apache.org/licenses/LICENSE-2.0.html
* Source: http://github.com/kwhinnery/Suds
*/
function SudsClient(_options) {
  function isBrowserEnvironment() {
    try {
      if (window && window.navigator) {
        return true;
      } else {
        return false;
      }
    } catch(e) {
      return false;
    }
  }

  function isAppceleratorTitanium() {
    try {
      if (Titanium) {
        return true;
      } else {
        return false;
      }
    } catch(e) {
      return false;
    }
  }

  //A generic extend function - thanks MooTools
  function extend(original, extended) {
    for (var key in (extended || {})) {
      if (original.hasOwnProperty(key)) {
        original[key] = extended[key];
      }
    }
    return original;
  }

  //Check if an object is an array
  function isArray(obj) {
    return Object.prototype.toString.call(obj) == '[object Array]';
  }

  //Grab an XMLHTTPRequest Object
  function getXHR() {
    var xhr;
    if (isBrowserEnvironment()) {
      if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
      }
      else {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
    else if (isAppceleratorTitanium()) {
      xhr = Titanium.Network.createHTTPClient();
    }
    return xhr;
  }

  //Parse a string and create an XML DOM object
  function xmlDomFromString(_xml) {
    var xmlDoc = null;
    if (isBrowserEnvironment()) {
      if (window.DOMParser) {
        parser = new DOMParser();
        xmlDoc = parser.parseFromString(_xml,"text/xml");
      }
      else {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(_xml); 
      }
    }
    else if (isAppceleratorTitanium()) {
      xmlDoc = Titanium.XML.parseString(_xml);
    }
    return xmlDoc;
  }

  // Convert a JavaScript object to an XML string - takes either an
  function convertToXml(_obj, namespacePrefix) {
    var xml = '';
    if (isArray(_obj)) {
      for (var i = 0; i < _obj.length; i++) {
        xml += convertToXml(_obj[i], namespacePrefix);
      }
    } else {
      //For now assuming we either have an array or an object graph
      for (var key in _obj) {
        if (namespacePrefix && namespacePrefix.length) {
          xml += '<' + namespacePrefix + ':' + key + '>';
        } else {
          xml += '<'+key+'>';
        }
        if (isArray(_obj[key]) || (typeof _obj[key] == 'object' && _obj[key] != null)) {
          xml += convertToXml(_obj[key]);
        }
        else {
          xml += _obj[key];
        }
        if (namespacePrefix && namespacePrefix.length) {
          xml += '</' + namespacePrefix + ':' + key + '>';
        } else {
          xml += '</'+key+'>';
        }
      }
    }
    return xml;
  }

  // Client Configuration
  var config = extend({
    endpoint:'http://localhost',
    targetNamespace: 'http://localhost',
    envelopeBegin: '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:ns0="PLACEHOLDER" 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:Header><ns0:Authentication><ns0:Username>Infrab</ns0:Username><ns0:Password>ZxH35oCeXnA</ns0:Password></ns0:Authentication></soap:Header><soap:Body>',
    envelopeEnd: '</soap:Body></soap:Envelope>'
  },_options);

  // Invoke a web service
  this.invoke = function(_soapAction,_body,_callback) {    
    //Build request body 
    var body = _body;

    //Allow straight string input for XML body - if not, build from object
    if (typeof body !== 'string') {
      body = '<ns0:'+_soapAction+'>';
      body += convertToXml(_body, 'ns0');
      body += '</ns0:'+_soapAction+'>';
    }

    var ebegin = config.envelopeBegin;
    config.envelopeBegin = ebegin.replace('PLACEHOLDER', config.targetNamespace);

    //Build Soapaction header - if no trailing slash in namespace, need to splice one in for soap action
    var soapAction = '';
    if (config.targetNamespace.lastIndexOf('/') != config.targetNamespace.length - 1) {
      soapAction = config.targetNamespace+'/'+_soapAction;
    }
    else {
      soapAction = config.targetNamespace+_soapAction;
    }

    //POST XML document to service endpoint
    var xhr = getXHR();
    xhr.onload = function() {
      _callback.call(this, xmlDomFromString(this.responseText));
    };
    xhr.open('POST',config.endpoint);
        xhr.setRequestHeader('Content-Type', 'text/xml');
        xhr.setRequestHeader('SOAPAction', soapAction);
        xhr.send(config.envelopeBegin+body+config.envelopeEnd);
        console.log(config.envelopeBegin+body+config.envelopeEnd);
  };
}

此代码中的主要方法是var config = extend({,它是SOAP xml的客户端配置。当我执行时,我得到这个输出:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring>Server was unable to process request. ---&gt; Object reference not set to an instance of an object.</faultstring>
<detail />
</soap:Fault>
</soap:Body>
</soap:Envelope>

我已经通过删除身份验证标头检查了SOAP UI以测试Web服务,它给了我与Titanium相同的确切错误。

我的问题是如何通过用户名和密码值传递身份验证标头?

0 个答案:

没有答案