自托管 WCF 上的 HTTPS 出现 400 错误
400 error with HTTPS over self hosted WCF
我正在尝试在自托管 windows 服务上实施 HTTPS。该服务是 RESTful(或试图成为)。使用常规 HTTP 的服务工作正常。但是当我切换到 HTTPS 时,它不会,我发送到该端口的任何 HTTPS 请求 return 都是 400 错误并且没有 logging/information.
尤其是这个。 (詹姆斯·奥斯本)。 http://blogs.msdn.com/b/james_osbornes_blog/archive/2010/12/10/selfhosting-a-wcf-service-over-https.aspx
使用后者,我能够将证书绑定到端口并使用他的测试控制台和应用程序,通过 HTTPS 进行通信。但是该应用程序在客户端和服务器上都有数据合同,而对我来说,我想使用网络浏览器发送 HTTPS 请求,所以这不太管用。
简而言之,我想通过 HTTPS 和 return "SUCCESS" 在 payload/browser window 中调用我的测试服务,但我收到了 400 错误没有任何细节。我很确定证书已绑定到端口,因为我通过 hTTPS 在该端口上使用了测试 server/client 并且它有效。
这是我的服务器代码。
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();
BasicHttpBinding 不能与 REST 服务一起使用。使用 WebHttpBinding 就像将其用于 HTTP 端点一样。
我正在尝试在自托管 windows 服务上实施 HTTPS。该服务是 RESTful(或试图成为)。使用常规 HTTP 的服务工作正常。但是当我切换到 HTTPS 时,它不会,我发送到该端口的任何 HTTPS 请求 return 都是 400 错误并且没有 logging/information.
尤其是这个。 (詹姆斯·奥斯本)。 http://blogs.msdn.com/b/james_osbornes_blog/archive/2010/12/10/selfhosting-a-wcf-service-over-https.aspx
使用后者,我能够将证书绑定到端口并使用他的测试控制台和应用程序,通过 HTTPS 进行通信。但是该应用程序在客户端和服务器上都有数据合同,而对我来说,我想使用网络浏览器发送 HTTPS 请求,所以这不太管用。
简而言之,我想通过 HTTPS 和 return "SUCCESS" 在 payload/browser window 中调用我的测试服务,但我收到了 400 错误没有任何细节。我很确定证书已绑定到端口,因为我通过 hTTPS 在该端口上使用了测试 server/client 并且它有效。
这是我的服务器代码。
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();
BasicHttpBinding 不能与 REST 服务一起使用。使用 WebHttpBinding 就像将其用于 HTTP 端点一样。