2009-06-04 141 views
11

在我们的项目中,我们有一个运行在http和https上的java web服务。 我们想要在内部使用http,并且为我们的Web应用程序的外部版本使用https。使用WCF通过Http和Https调用Web服务

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

我们需要对代码和配置进行哪些更改才能在我们的外部应用程序中为同一服务支持https?如果可能请提供代码片段来解释!

回答

0

请参阅Configuring HTTP and HTTPS

Using Windows Communication Foundation (WCF) over HTTP either requires the use of a host, such as Internet Information Services (IIS), or manual configuration of the HTTP settings through the HTTP Server API. This document describes manually configuring WCF when using HTTP and HTTPS.

而且还看到WCF Bindings Needed For HTTPS

I just finished writing my first production WCF application, which worked very well until I deployed it to our production environment. All of a sudden none of the WCF calls would work, and I would get a JavaScript "TestService is not defined" error. When I look inside the JS service reference (in debug mode), I got the following error:

Could not find a base address that matches scheme http for the endpoint with binding WebHttpBinding. Registered base address schemes are [https]

So apparently my WCF service registered itself as HTTPS (since it is over SSL), but my binding was only configured for HTTP. The solution is to define a custom binding inside your Web.Config file and set the security mode to "Transport". Then you just need to use the bindingConfiguration property inside your endpoint definition to point to your custom binding. The entire HTTPS-enabled system.serviceModel section is below:

0

我知道你正在使用WCF来构建客户,连接到远程web服务,通​​过HTTPS。

为此,只需在configuration/system.serviceModel/client/endpoint/address下修改WCF驱动应用的客户端配置文件,替换http://server.addresshttps://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中,但它应该是可以改变的。代理也是。

+0

这并不适用于我的情况下工作,它会导致系统。带有消息的ArgumentException“提供的URI方案'https'无效;期望'http'。” – RenniePet 2014-11-21 06:24:58

2

我假设你正在使用basichttpbinding。然后,你需要做两两件事:

  • 更改地址为https :)
  • 设置安全模式运输
+0

不错 - 链接到相同的问题:) – Rory 2011-02-13 19:41:52

+0

@Rory,很好的接收,答案已经有一年多了,你是第一个注意到:) – 2011-02-13 19:49:07

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