无法从IIS托管的应用程序连接到远程REST服务

时间:2018-07-12 17:25:38

标签: c# rest iis soap https

我的计算机上的IIS中托管了一个SOAP Web服务。该SOAP服务有几种方法可以与使用基本身份验证的远程REST服务进行交互。

我使用以下代码从SOAP服务向REST服务发出POST请求:

HttpClient serviceClient;
serviceClient = new HttpClient {BaseAddress = new Uri("https://myremoterestservice.com:9981/callme/")};
serviceClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
            "Basic",
            Convert.ToBase64String(
                Encoding.ASCII.GetBytes(
                    $"myuser:mypassword")
            )
        );

string postXml = "my request XML";
Task<HttpResponseMessage> task = serviceClient.PostAsync("methodname", new StringContent(postXml, Encoding.UTF8, "application/xml");
// I need to get the result synchronously
using (var response = task.Result) // exception "A Task was cancelled" thrown here
{
    using (var content = response.Content)
    {
        // ....
    }
}

一切都与使用HTTP的不同远程REST服务一起使用。但是,当我尝试为使用HTTPS的REST服务调用相同的方法时,此代码在using (var response = task.Result)

行中引发异常,并显示消息“任务已取消”

但是,这仅在我通过SOAP Web服务调用该代码时不起作用-甚至无法与远程REST服务建立连接。当我从单元测试中调用相同的代码时,它可以工作。

我认为问题可能出在IIS设置中或我的SOAP Web服务的web.config文件中,但我不知道确切的位置。

更新: 我注意到在Visual Studio的单元测试中运行此请求时使用了TLS 1.2。当我从IIS托管的SOAP Web服务运行它时,将使用TLS 1.0。

1 个答案:

答案 0 :(得分:0)

我决定将使用Visual Studio发出的请求与SOAP Web服务发出的请求进行比较,并发现TLS版本存在差异:运行请求时使用TLS 1.2使用Visual Studio,并且Web服务使用了TLS 1.0。我发现问题出在我的SOAP Web服务的web.config文件中。在web.config中指定了以下设置:

<httpRuntime targetFramework="4.5.2" />

仅从.NET 4.6开始默认支持TLS 1.2。因此,当我将httpRuntime targetFramework更改为4.7时,一切开始起作用。似乎远程REST服务已配置为不允许TLS 1.0进行握手。