【问题标题】:400 error with HTTPS over self hosted WCF通过自托管 WCF 的 HTTPS 出现 400 错误
【发布时间】:2015-03-14 09:43:43
【问题描述】:

我正在尝试在自托管 Windows 服务上实现 HTTPS。该服务是 RESTful 的(或试图成为)。使用常规 HTTP 的服务运行良好。但是当我切换到 HTTPS 时,它不会,我发送到该端口的任何 HTTPS 请求都会返回 400 错误并且没有日志记录/信息。

我看了这个参考https://pfelix.wordpress.com/2011/04/21/wcf-web-api-self-hosting-https-and-http-basic-authentication/

尤其是这个。 (詹姆斯·奥斯本)。 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();

【问题讨论】:

    标签: c# wcf rest https


    【解决方案1】:

    BasicHttpBinding 不适用于 REST 服务。使用 WebHttpBinding,就像在 HTTP 端点中使用它一样。

    【讨论】:

    • 非常感谢,这是正确的解决方法。现在可以使用了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 2015-02-17
    • 2013-01-14
    相关资源
    最近更新 更多