2009-06-04 140 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