【发布时间】:2015-03-14 09:43:43
【问题描述】:
我正在尝试在自托管 Windows 服务上实现 HTTPS。该服务是 RESTful 的(或试图成为)。使用常规 HTTP 的服务运行良好。但是当我切换到 HTTPS 时,它不会,我发送到该端口的任何 HTTPS 请求都会返回 400 错误并且没有日志记录/信息。
尤其是这个。 (詹姆斯·奥斯本)。 http://blogs.msdn.com/b/james_osbornes_blog/archive/2010/12/10/selfhosting-a-wcf-service-over-https.aspx
使用后者,我能够将证书绑定到端口并使用他的测试控制台和应用程序,通过 HTTPS 进行通信。但是该应用程序在客户端和服务器上都有一个数据合同,而对我来说,我想使用 Web 浏览器发送 HTTPS 请求,所以这不太有效。
简而言之,我想通过 HTTPS 调用我的测试服务并在有效负载/浏览器窗口中返回“SUCCESS”,但我得到了一个没有任何详细信息的 400 错误。我很确定证书已绑定到端口,因为我通过 hTTPS 在该端口上使用了测试服务器/客户端并且它可以工作。
这是我的服务器代码。
private void StartWebService()
{
Config.ReadConfig();
String port = Config.ServicePort;
eventLog1.WriteEntry("Listening on port" + port);
//BasicHttpBinding binding = new BasicHttpBinding();
//binding.Security.Mode = BasicHttpSecurityMode.Transport;
// THESE LINES FOR HTTPS
Uri httpsUrl = new Uri("https://localhost:" + port + "/");
host = new WebServiceHost(typeof(WebService), httpsUrl);
BasicHttpBinding binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.Transport;
// THIS IS FOR NORMAL HTTP
//Uri httpUrl = new Uri("http://localhost:" + port + "/");
//host = new WebServiceHost(typeof(WebService), httpUrl);
//var binding = new WebHttpBinding(); // NetTcpBinding();
host.AddServiceEndpoint(typeof(iContract), binding, "");
ServiceDebugBehavior stp = host.Description.Behaviors.Find<ServiceDebugBehavior>();
stp.HttpHelpPageEnabled = false;
host.Open();
}
这里是网络服务
class WebService : iContract
{
public string TestMethod()
{
return "SUCCESS";
}
public string HelloWorld()
{
return "SUCCESS";
}
这里是 iContract
[ServiceContract]
interface iContract
{
[OperationContract]
[WebGet]
string TestMethod();
[WebInvoke(Method = "GET",
UriTemplate = "HelloWorld",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped)]
Stream HelloWorld();
【问题讨论】: