2011-12-20 89 views
3

我需要通過HTTPS從第三方服務器調用一些REST服務。我的想法是,我會自己創建一個漂亮的小WCF庫來處理這些調用並對反應進行反序列化。儘管如此,我還是會有些失落。從WCF調用HTTPS REST服務

我試圖調用的服務有一個測試服務,只是迴應OK。

我已經創建了我的界面中的OperationContract的,如下圖所示:

[OperationContract] 
[WebGet(BodyStyle = WebMessageBodyStyle.Bare, 
    ResponseFormat = WebMessageFormat.Xml, 
    UriTemplate = "test")] 
string Test(); 

在我的服務代碼,下面我有一個公共方法:

public string Test() 
{ 
    ChannelFactory<IService1> factory = new ChannelFactory<IService1>("UMS"); 
    var proxy = factory.CreateChannel(); 
    var response = proxy.Test(); 
    ((IDisposable)proxy).Dispose(); 
    return (string)response; 
} 

我的app.config如下:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.web> 
    <compilation debug="true" /> 
    </system.web> 
    <system.serviceModel> 
    <client> 
     <endpoint address="https://61.61.34.19/serv/ums/" 
       binding="webHttpBinding" 
       behaviorConfiguration="ums" 
       contract="WCFTest.IService1" 
       bindingConfiguration="webBinding" 
       name="UMS" /> 
    </client> 
    <services> 
     <service name="WCFTest.Service1" behaviorConfiguration="WCFTest.Service1Behavior"> 
     <host> 
      <baseAddresses> 
      <add baseAddress = "http://localhost:8731/Design_Time_Addresses/WCFTest/Service1/" /> 
      </baseAddresses> 
     </host> 
     <endpoint address ="" binding="wsHttpBinding" contract="WCFTest.IService1"> 
      <identity> 
      <dns value="localhost"/> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
     </service> 
    </services> 
    <behaviors> 
     <endpointBehaviors> 
     <behavior name="ums"> 
      <clientCredentials> 
      <clientCertificate findValue="restapi.ext-ags.801" 
           storeLocation="CurrentUser" 
           x509FindType="FindBySubjectName"/> 
      </clientCredentials> 
      <webHttp/> 
     </behavior> 
     </endpointBehaviors> 
     <serviceBehaviors> 
     <behavior name="WCFTest.Service1Behavior"> 
      <serviceMetadata httpGetEnabled="True"/> 
      <serviceDebug includeExceptionDetailInFaults="False" /> 
     </behavior>  
     </serviceBehaviors> 
    </behaviors> 
    <bindings> 
     <webHttpBinding> 
     <binding name="webBinding"> 
      <security mode="Transport"> 
      <transport clientCredentialType="Certificate" proxyCredentialType="None"/> 
      </security> 
     </binding> 
     </webHttpBinding> 
    </bindings> 
    </system.serviceModel> 
</configuration> 

我嘗試調用測試方法,但收到錯誤「無法建立信任關係nship爲SSL/TLS安全通道,授權爲'61 .61.34.19'。「

任何人都可以看到我在這裏失蹤?

任何幫助表示讚賞。乾杯。

+0

你可以從你的IE瀏覽到REST服務,看到迴應?客戶端認證是否由REST服務使用客戶端證書完成? – Rajesh 2011-12-21 09:51:51

+0

是Rajesh,我可以從IE瀏覽並查看響應。 – 2011-12-21 10:27:05

+0

如果您的證書是自簽名的,那麼您是否實施了驗證服務器證書的ServicePointManager回調方法。您是否也正在從您嘗試測試的客戶端機器瀏覽服務? – Rajesh 2011-12-21 10:33:14

回答

1

無法爲權限爲'61 .61.34.19'的SSL/TLS安全通道建立信任關係。

這通常意味着服務器的SSL證書存在問題。是否自簽名?如果是這樣,你必須建立信任的證書,通常由trusting the issuing CA or installing the cert in the windows cert store

+0

Cheers Randolph,證書安裝在商店中,如果我點擊測試服務的URI在IE中我可以看到結果,只是努力從WCF執行相同的操作。還嘗試通過詢問cert store直接從bog標準WPF應用程序調用,找到證書並添加到HttpWebRequest.CientCertificates集合,然後調用.GetResponse()作爲HttpWebResponse – 2011-12-21 09:28:42

0

嘗試將webHttpBinding安全設置從「傳輸」更改爲「無」。