[英]Unable to connect to the remote REST service from the IIS-hosted application
I have a SOAP web service that is hosted in IIS on my machine. 我的计算机上的IIS中托管了一个SOAP Web服务。 This SOAP service has several methods which may interact with the remote REST service that uses Basic authentication.
此SOAP服务有几种方法可以与使用基本身份验证的远程REST服务进行交互。
I use the following code to make a POST request to the REST service from my SOAP service: 我使用以下代码从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)
{
// ....
}
}
Everything works with the different remote REST service that uses HTTP. 一切都可以与使用HTTP的不同远程REST服务一起使用。 But when I try to call the same method for the REST service that uses HTTPS, this code throws an exception with the message "A task was cancelled" at the line
using (var response = task.Result)
但是,当我尝试为使用HTTPS的REST服务调用相同的方法时,此代码
using (var response = task.Result)
在行上引发异常,并显示消息“任务已取消”。
But this doesn't work only when I'm calling that code via my SOAP web service - it fails even to establish a connection with the remote REST service. 但这仅在我通过SOAP Web服务调用该代码时不起作用-甚至无法与远程REST服务建立连接。 When I'm calling the same code from my unit-tests, it works.
当我从单元测试中调用相同的代码时,它可以工作。
I think the issue may be somewhere in the IIS settings or in the web.config file of my SOAP web service, but I don't have an idea where exactly. 我认为问题可能出在IIS设置中或我的SOAP Web服务的web.config文件中,但是我不知道确切的位置。
UPDATE: I noticed that TLS 1.2 is used when I'm running this request from my unit-tests in visual studio. 更新:我注意到在Visual Studio的单元测试中运行此请求时使用了TLS 1.2。 When I'm running it from my IIS-hosted SOAP web service, TLS 1.0 is used.
当我从IIS托管的SOAP Web服务运行它时,将使用TLS 1.0。
I decided to compare request that I made using the Visual Studio with the request that was made by the SOAP web-service, and I noticed that there was a difference in a TLS version: TLS 1.2 was used when I run the request using Visual Studio, and the TLS 1.0 was used by the web service. 我决定将使用Visual Studio发出的请求与SOAP Web服务发出的请求进行比较,然后发现TLS版本有所不同:使用Visual Studio运行请求时使用TLS 1.2 ,并且Web服务使用了TLS 1.0。 I found that the issue was in the web.config file of my SOAP web service.
我发现问题出在我的SOAP Web服务的web.config文件中。 The following setting was specified in the web.config:
在web.config中指定了以下设置:
<httpRuntime targetFramework="4.5.2" />
TLS 1.2 was supported by default only since .NET 4.6. 从.NET 4.6开始,默认情况下才支持TLS 1.2。 So, when I changed httpRuntime targetFramework to 4.7, everything started to work.
因此,当我将httpRuntime targetFramework更改为4.7时,一切开始起作用。 It seems that the remote REST service was configured to not allow TLS 1.0 to make a handshake.
似乎将远程REST服务配置为不允许TLS 1.0进行握手。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.