我编写了一个返回 JSON 响应的 WCF 服务。然后我用一些 JavaScript 代码创建了一个 html 页面来测试这个功能。当我将服务发布到登台环境(使用 SSL 来模拟生产环境)时,我必须更新我的服务的 web.config 文件以通过 HTTPS 工作。当我直接浏览到 .svc 端点(服务页面以 http 和 https 显示)以及在浏览器中调用服务时(我被提示下载 JSON 响应),一切似乎都正常,但是当我将测试页面更改为指向 https 版本,我收到“拒绝访问”错误。
这是我的配置文件的 servicemodel 部分的代码:
<system.serviceModel>
<services>
<service name="Services.PromotionCodeGeneration" behaviorConfiguration="md">
<endpoint address="" binding="webHttpBinding" contract="Services.IPromotionCodeGeneration" behaviorConfiguration="webHttp" bindingConfiguration="webBindingSecure"/>
<endpoint address="" binding="webHttpBinding" contract="Services.IPromotionCodeGeneration" behaviorConfiguration="webHttp" bindingConfiguration="webBinding"/>
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="md">
<serviceMetadata httpsGetEnabled="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webHttp">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="webBindingSecure">
<security mode="Transport"/>
</binding>
<binding name="webBinding">
<security mode="None"></security>
</binding>
</webHttpBinding>
</bindings>
</system.serviceModel>
这是我的测试页面中的代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title></title>
<script>
function api_test() {
var url = "https://test.mydomain.com/Services/PromotionCodeGeneration.svc/contest/useremail@mydomain.com";
var client = new XMLHttpRequest();
client.open("GET", url, false);
client.setRequestHeader("Authentication-Key", "LK589-JIJO-SHG9-0987-65TG-HJKU-Y$GH");
client.send();
var responseText = client.responseText;
var result = JSON.parse(responseText);
document.writeln("Id: " + result.Id + "<br/>");
document.writeln("Code: " + result.Code + "<br/>");
var expiryDate = "";
if (result.Expires != null){expiryDate = result.Expires;}
document.writeln("Expires: " + expiryDate + "<br/>");
document.writeln("Status: " + result.Status + "<br/>");
}
</script>
</head>
<body onload="api_test();">
</body>
</html>
我已经研究了2天的问题。我发现很多人说你不能跨域使用“XMLHttpRequest”方法,但它对我来说比基本的 http 更有效,所以我觉得这很难相信。我也尝试了许多不同的服务模型配置建议,但没有一个适用于 https 通信。是否有人在我的配置或调用页面中看到任何通过 https 导致“拒绝访问”响应的内容?
任何帮助,将不胜感激。