【问题标题】:Unable to connect to the remote REST service from the IIS-hosted application无法从 IIS 托管的应用程序连接到远程 REST 服务
【发布时间】:2018-12-21 00:33:20
【问题描述】:

我的机器上有一个托管在 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。

【问题讨论】:

  • 如果你使用task.GetAwaiter().GetResult();会怎样?
  • 我也试过了,但没有任何改变

标签: c# rest iis soap https


【解决方案1】:

我决定将使用 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 进行握手。

【讨论】:

    猜你喜欢
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-26
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 2015-03-17
    相关资源
    最近更新 更多