【发布时间】: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();会怎样? -
我也试过了,但没有任何改变