2010-01-25 42 views
1

我打算開發一個firefox擴展,它將XMLHttpRequest設置爲this WebService。通過來自Firefox擴展的SSL的X​​MLHttpRequest

我可以用下面的代碼(從overlay.js中)正確查詢服務:

var req = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:dat=\"http://webservice.whereisnow.com/datatypes\"><soapenv:Header/><soapenv:Body><dat:CurrentDocument><dat:applicationId>1</dat:applicationId><dat:publisherId>84</dat:publisherId><dat:documentId>8</dat:documentId><dat:versionId>1</dat:versionId></dat:CurrentDocument></soapenv:Body></soapenv:Envelope>"; 
var xmlhttpreq = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Components.interfaces.nsIXMLHttpRequest); 

xmlhttpreq.open("POST","https://www.whereisnow.com/webservice",false); // false = async 
xmlhttpreq.channel.loadFlags |= Components.interfaces.nsIRequest.LOAD_BYPASS_CACHE;  
xmlhttpreq.send(req); 
if (xmlhttpreq.readyState == 4){ 
    if(xmlhttpreq.status == 200) 
     alert(xmlhttpreq.responseText); 
    else 
     alert("Error: xmlhttpreq.status = " + xmlhttpreq.status) 
    } 
else 
    alert("Not ready: xmlHttpreq.readyState = " + xmlHttpreq.readyState); 

這個responseText將是一個有效的XML,我可以操作,但WebService既可以通過HTTP訪問和https(SSL),我必須通過https執行此操作,因爲需要身份驗證才能執行某些操作。

爲了獲得通過https我必須修改使用HTTPS端點://而不是http://並以這種方式請求XML:

var req = "<soapenv:Envelope xmlns:dat=\"http://webservice.whereisnow.com/datatypes\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Header><wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\"><wsse:UsernameToken wsu:Id=\"UsernameToken-1\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\"><wsse:Username>MY_USERNAME</wsse:Username><wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText\">SHA1_OF_MY_PASSWORD</wsse:Password></wsse:UsernameToken></wsse:Security></soapenv:Header><soapenv:Body><dat:CurrentDocument><dat:applicationId>1</dat:applicationId><dat:publisherId>84</dat:publisherId><dat:documentId>10</dat:documentId><dat:versionId>1</dat:versionId></dat:CurrentDocument></soapenv:Body></soapenv:Envelope>"; 

的問題是,服務器的狀態始終是500和responseText永遠是這樣的:

<?xml version='1.0' encoding='UTF-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><soapenv:Fault><faultcode>soapenv:Server</faultcode><faultstring>InvalidSecurity</faultstring><detail></detail></soapenv:Fault></soapenv:Body></soapenv:Envelope> 

任何幫助?


編輯

this url發現代碼中,我發現服務的SSL證書工作正常。所以我覺得這個問題是不是HTTPS ...

回答

0

解決開放後添加以下行(...):

xmlhttpreq.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); 
xmlhttpreq.setRequestHeader("SOAPAction", "whereIsNow");