Error retrieving access_token

I am using the following code to retrieve an access_token and I get an error “The underlying connection was closed: An unexpected error occurred on a send”… any idea why?

public static string InfusionsoftUrl = “”;

public class AccessTokenRequest
public string client_id { get; set; }
public string client_secret { get; set; }
public string code { get; set; }
public string grant_type { get; set; }
public string redirect_uri { get; set; }
public class AccessTokenResponse
public string access_token { get; set; }
public string refresh_token { get; set; }
public long expires_in { get; set; }
public void CreateOrUpdateAccessToken(string urlLeftPart, string code)
var request = new AccessTokenRequest()
client_id = “myclientid”,
client_secret = “myclientsecret”,
code = code,
grant_type = “authorization_code”,
redirect_uri = urlLeftPart + “/callback/infusionsoft”,
using (var client = new HttpClient())
string json = JsonConvert.SerializeObject(request);
var content = new StringContent(json, Encoding.UTF8, “application/json”);
var response = client.PostAsync(InfusionsoftUrl + “/token”, content).Result;
var jsonResponse = response.Content.ReadAsStringAsync().Result;
var result = JsonConvert.DeserializeObject(jsonResponse);

Typically the error that you’re getting occurs if you’re attempting to make the request while using the wrong security protocol. You’ll want to make sure that your server is using TLS 1.2 or above. You may need to specify in your code that TLS 1.2 needs to be used. For example, .NET 4.5 supports TLS 1.2, but defaults to using 1.1, while .NET 4.6 uses 1.2 as default.

Thanks!.. that fixed my problem… I also had to change the request to FormUrlEncodedContent.

Perfect, glad that worked!