我需要通過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'。「
任何人都可以看到我在這裏失蹤?
任何幫助表示讚賞。乾杯。
你可以從你的IE瀏覽到REST服務,看到迴應?客戶端認證是否由REST服務使用客戶端證書完成? – Rajesh 2011-12-21 09:51:51
是Rajesh,我可以從IE瀏覽並查看響應。 – 2011-12-21 10:27:05
如果您的證書是自簽名的,那麼您是否實施了驗證服務器證書的ServicePointManager回調方法。您是否也正在從您嘗試測試的客戶端機器瀏覽服務? – Rajesh 2011-12-21 10:33:14