从WCF调用HTTPS REST服务

3

我需要从第三方服务器上通过HTTPS调用一些REST服务。我的想法是创建一个漂亮的小WCF库来处理这些调用并反序列化响应。但我遇到了一些困难。

我试图调用的服务有一个测试服务,只是简单地回复“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>

我尝试调用测试方法,但收到错误消息“无法建立与'61.61.34.19'的SSL/TLS安全通道的信任关系。”

有人能看出我漏了什么吗?

感谢任何帮助。谢谢。


你能否在IE中浏览REST服务并查看响应?REST服务是否使用客户端证书进行客户端身份验证? - Rajesh
是的,Rajesh,我可以从IE浏览器中浏览并查看响应。 - P_S_A_M
如果您的证书是自签名的,那么您是否实现了ServicePointManager回调方法来验证服务器证书?另外,您是否从客户端机器浏览到正在尝试测试的服务? - Rajesh
H Rajesh。证书是由我需要使用其服务的第三方提供的。我在我的开发机器上,服务在其他地方的服务器上。 - P_S_A_M
你确定在从你的机器发出请求时,客户端证书已经被发送了吗?你可以在测试方法中这样做 --> factory.Credentials.ClientCertificate = "你的客户端证书",然后看看你的请求是否成功。 - Rajesh
2个回答

1

谢谢 Randolph,证书已经安装到存储中,如果我在IE中输入测试服务的URI,可以看到结果,但是我无法从WCF中执行相同操作。我还尝试过通过查询证书存储库、定位证书并将其添加到 HttpWebRequest.CientCertificates 集合中,然后调用 .GetResponse() 作为 HttpWebResponse,直接从一个标准的 WPF 应用程序进行调用。 - P_S_A_M

0

尝试将webHttpBinding安全设置从“Transport”更改为“None”。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接