HTTPS通过自托管WCF发生400错误

时间:2015-01-15 21:01:13

标签: c# wcf rest https

我正在尝试在自托管的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();

}

这是WebService

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();

1 个答案:

答案 0 :(得分:1)

BasicHttpBinding不适用于REST服务。像使用HTTP端点一样使用WebHttpBinding。

相关问题