【问题标题】:XMLHttpRequest over SSL from Firefox extension来自 Firefox 扩展的基于 SSL 的 XMLHttpRequest
【发布时间】:2010-01-25 16:00:59
【问题描述】:

我将开发一个 firefox 扩展,它向this WebService 发出 XMLHttpRequest。

我可以使用以下代码(来自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...

【问题讨论】:

    标签: javascript firefox ssl xmlhttprequest


    【解决方案1】:

    解决了在 open(...) 之后添加以下行:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多