使用WCF通过Http和Https调用Web服务

12
在我们的项目中,我们有一个运行在 http 和 https 上的 Java web 服务。 我们希望在内部使用 http,而对于我们的 Web 应用的外部版本则使用 https。
因此,我们在应用程序中创建了代理类,并在 web/app.config 中设置了 http 的绑定,一切正常运行。
如果要在我们的外部应用程序中支持相同的服务的 https 版本,需要对代码和配置进行哪些更改?如果可能,请提供代码片段来解释!
4个回答

22

我在MSDN上找到了答案。

在我的情况下,我使用了自定义绑定:

<customBinding>
    <binding name="jsonpBinding">
        <jsonpMessageEncoding/>
        <httpTransport manualAddressing="true"/>
    </binding>
</customBinding>

这是在服务中被引用的。

<services>
    <service name="{YourInfoHere}">
        <endpoint address="" binding="customBinding" bindingConfiguration="jsonpBinding" behaviorConfiguration="{YourInfoHere}" contract="{YourInfoHere}"/>
    </service>
</services>

添加了使用httpsTransport的第二个绑定,然后使用该绑定的第二个服务解决了问题。最终输出:

    <services>
        <service name="{YourInfoHere}">
            <endpoint address="" binding="customBinding" bindingConfiguration="jsonpBinding" behaviorConfiguration="{YourInfoHere}" contract="{YourInfoHere}"/>
            <endpoint address="" binding="customBinding" bindingConfiguration="jsonpBindingHttps" behaviorConfiguration="{YourInfoHere}" contract="{YourInfoHere}"/>
        </service>
    </services>
    <bindings>
        <customBinding>
            <binding name="jsonpBinding">
                <jsonpMessageEncoding/>
                <httpTransport manualAddressing="true"/>
            </binding>
            <binding name="jsonpBindingHttps">
                <jsonpMessageEncoding/>
                <httpsTransport manualAddressing="true" />
            </binding>
        </customBinding>
    </bindings>

可能不是理想的方法,但它能够工作。这些是我为使SSL工作所做的唯一更改。由于所有内容都在绑定和传输中,因此代码保持不变。

相关的MSDN链接:

  1. 自定义绑定:http://msdn.microsoft.com/en-us/library/ms731377.aspx
  2. HttpTransport:http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.httptransportbindingelement.aspx
  3. HttpsTransport:http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.httpstransportbindingelement.aspx

2

我假设您正在使用basichttpbinding。那么您需要做两件事:

  • 将地址更改为https :)
  • 将安全模式设置为传输

很好 - 链接到相同的问题 :) - Rory
@Rory,干得好,答案已经在那里超过一年了,你是第一个注意到它的人 :) - Shiraz Bhaiji
虽然你的回答很明显,但它可能为我节省了数小时。我假设你正在使用basichttpbinding。我的SSL适用于SOAP,但对于webHttpBinding-REST则不适用。现在我已经确定了问题,可以开始修复 :) - IAmGroot

0

我了解您正在使用WCF构建客户端,该客户端通过HTTPS连接到远程Web服务。

为此,只需修改WCF应用程序的客户端配置文件,在configuration/system.serviceModel/client/endpoint/@address中将http://server.address替换为https://server.address,如下所示:

<configuration>
  <system.serviceModel>
    ...
    <client>
        <!-- change only the address string -->
        <endpoint address="https://server.name/Whatever"
            everything.else.stays.the.same />

    </client>
  </system.serviceModel>
</configuration>

配置文件的路径根据常规的.NET规则而异:无论是ASPNET应用程序、服务还是其他类型。

或者您可以在代码中明确设置地址:

    // instantiate new proxy to web service
    var svc= new ServiceClient();
    var e = svc.Endpoint;
    e.Address = new System.ServiceModel.EndpointAddress("https://server.address/JavaServiceUri");

我强烈建议您将地址设置为可配置的,而不是硬编码。这并不意味着它必须存储在app.config中,但应该是可更改的。代理也是如此。


这对我的情况不起作用,它会导致一个System.ArgumentException错误,错误信息为“提供的URI方案'https'无效;预期为'http'。” - RenniePet

0

请参阅配置HTTP和HTTPS

使用Windows Communication Foundation(WCF)通过HTTP进行通信,要么需要使用主机(例如Internet Information Services(IIS)),要么需要通过HTTP Server API手动配置HTTP设置。本文档描述了在使用HTTP和HTTPS时手动配置WCF。

还请参阅HTTPS所需的WCF绑定

我刚刚写完了我的第一个生产WCF应用程序,它在部署到我们的生产环境之前一直很好地运行。突然间所有的WCF调用都无法工作,我会收到一个JavaScript错误提示"TestService未定义"。当我检查JS服务引用时(在调试模式下),我得到了以下错误: “找不到与WebHttpBinding绑定的端点匹配的基址的方案http。注册的基址方案是[https]。” 显然,我的WCF服务注册为HTTPS(因为它是通过SSL进行的),但我的绑定只配置了HTTP。解决办法是在Web.Config文件中定义一个自定义绑定,并将安全模式设置为“Transport”。然后,您只需要在终结点定义中使用bindingConfiguration属性来指向自定义绑定即可。整个启用HTTPS的system.serviceModel部分如下:

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接