ClientBase和https连接。提供的URI方案“https”无效;预计'http'。参数名称:via

时间:2012-05-08 13:10:03

标签: c# wcf rest client

我尝试使用ClientBase类与REST服务进行通信。以下是我的代码:

class MyService: ClientBase<IMyService>, IMyService
{
    public GridVisionService(Uri baseAddress)
        : base(new WebHttpBinding(), new EndpointAddress(baseAddress))
    {
        this.Endpoint.Behaviors.Add(new WebHttpBehavior());
    }

    #region IMyServiceMembers

    public void DoSomething()
    {
        using (new OperationContextScope(this.InnerChannel)) // Line that throws the exception
        {
            WebOperationContext.Current.OutgoingRequest.Headers.Add("details", "details");
        } // End of OperationContextScope

        this.Channel.DoSomething();
    }

    #endregion
}

我按照以下方式运行我的课程:

MyService myService = new MyService(new Uri("https://localhost:8080/MyService/"));

但是,它会引发关于期望HTTP而不是HTTPS的异常。在搜索时,我发现了同一错误的多个实例,但在所有情况下,我发现app.config正在配置连接。就我而言,我不是。有谁知道如何让我的服务期望使用https?

1 个答案:

答案 0 :(得分:3)

见[this post] [1]。

创建WebHttpBinding时,将其Security属性设置为transport。

System.ServiceModel.WebHttpBinding w = new System.ServiceModel.WebHttpBinding();
w.Security = new System.ServiceModel.WebHttpSecurity();
w.Security.Mode = System.ServiceModel.WebHttpSecurityMode.Transport;