【问题标题】:calling a web service using WCF over Http and Https使用 WCF over Http 和 Https 调用 Web 服务
【发布时间】:2010-10-31 08:59:08
【问题描述】:

在我们的项目中,我们有一个通过 http 和 https 运行的 java web 服务。 我们希望在内部使用 http,在外部版本的 Web 应用程序中使用 https。

所以我们已经在我们的应用程序中创建了代理类,并且我们已经在 web/app.config 中设置了 http 的绑定并且一切正常。

我们需要对代码和配置进行哪些更改以支持外部应用程序中相同服务的 https?可以的话请提供代码sn-ps来解释!

【问题讨论】:

    标签: asp.net wcf http https


    【解决方案1】:

    请看Configuring HTTP and HTTPS:

    使用 Windows Communication Foundation (WCF) over HTTP 要么需要 使用主机,例如 Internet 信息服务 (IIS),或手册 HTTP设置的配置 通过 HTTP 服务器 API。这 文档手动描述 使用 HTTP 时配置 WCF 和 HTTPS。

    另见WCF Bindings Needed For HTTPS:

    我刚写完我的第一篇 生产 WCF 应用程序,其中 在我部署它之前工作得很好 到我们的生产环境。所有的 突然之间没有 WCF 调用会 工作,我会得到一个 JavaScript “未定义测试服务”错误。 当我查看 JS 服务内部时 参考(在调试模式下),我得到了 以下错误:

    找不到与端点的方案 http 匹配的基地址 与绑定 WebHttpBinding。 注册的基地址方案是 [https]

    显然我的 WCF 服务 将自己注册为 HTTPS(因为它 是通过 SSL),但我的绑定只是 为 HTTP 配置。解决方案是 在您的内部定义自定义绑定 Web.Config 文件并设置安全性 模式为“运输”。那你就 需要使用 bindingConfiguration 端点内的属性 定义指向您的自定义 捆绑。整个启用 HTTPS system.serviceModel 部分如下:

    【讨论】:

      【解决方案2】:

      我了解到您正在使用 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'。”
      【解决方案3】:

      我假设您使用的是 basichttpbinding。然后你需要做两件事:

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

      【讨论】:

      • 很好 - 链接到同一个问题:)
      • @Rory,很好,答案已经有一年多了,你是第一个注意到的:)
      • 你的回答很明显,它只是为我节省了几个小时。 I am assuming that you are using basichttpbinding. 我的 SLL 适用于 SOAP,但不适用于 webHttpBinding -REST。现在我发现了问题,我可以开始修复了:)
      【解决方案4】:

      我在 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

      【讨论】:

        猜你喜欢
        • 2011-12-03
        • 2011-08-03
        • 2023-03-25
        • 2014-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-02
        • 2016-06-30
        相关资源
        最近更新 更多