Al Mills
Al Mills

Reputation: 1112

HttpWebRequest call to GetResponse, fails using .net 4.5 but passes using net 4.6

We've just launched a new service, which we're having trouble connecting to from a very basic c# console app when targeting the .Net 4.5 framework.

We first found the problem in an ASP MVC site, but have broken it down into the simplest of simple console apps to help isolate the problem

Code snippet (there isn't anything else):

string myURL = @"https://<myurl>.com/<myurl>";
using (var httpClient = new HttpClient())
{                
    var request = (HttpWebRequest)WebRequest.Create(myURL);
    request.Method = "GET";
    request.ContentLength = 0;
    request.ContentType = "text/xml";

    using (var response = (HttpWebResponse)request.GetResponse())
    {
        if (response.StatusCode != HttpStatusCode.OK)
        {
            ...
        }
        else
        {
            ...
        }
    }                
}

What happens

Service Information

Other differences

Setup Essentials

Has it ever worked?

Yes!

What am I asking

Thanks, Al.

Upvotes: 3

Views: 2072

Answers (1)

Pikoh
Pikoh

Reputation: 7713

This is usually caused by the server using TLS v1.2. I think Net4.5 defaults to TLS v1.1, so you must add this to your code:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

Upvotes: 5

Related Questions