简体   繁体   中英

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. This SOAP service has several methods which may interact with the remote REST service that uses Basic authentication.

I use the following code to make a POST request to the REST service from my SOAP service:

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. 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)

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. 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.

UPDATE: I noticed that TLS 1.2 is used when I'm running this request from my unit-tests in visual studio. When I'm running it from my IIS-hosted SOAP web service, TLS 1.0 is used.

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. I found that the issue was in the web.config file of my SOAP web service. The following setting was specified in the web.config:

<httpRuntime targetFramework="4.5.2" />

TLS 1.2 was supported by default only since .NET 4.6. So, when I changed httpRuntime targetFramework to 4.7, everything started to work. It seems that the remote REST service was configured to not allow TLS 1.0 to make a handshake.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM