We are extending the source code of a module that targets .NET Framework version 4.5. Our intent is to use System.Net.Http.HttpClient to send relevant information to a payment API and obtain the response. However, our diagnostics have shown the following error:
Exception in HttpClientHandler#31129591::SendAsync - The underlying connection was closed: An unexpected error occurred on a send..
I understand that .NET Framework 4.5 uses an older Transport Layer Security (TLS) than more modern APIs utilize. I'm thinking this is a root cause but am unsure whether there's something else amiss. The code that calls this API is as follows:
HttpClientHandler handler = new HttpClientHandler
{
AllowAutoRedirect = true
};
TransactionResult_ProPay result = new TransactionResult_ProPay();
using (HttpClient client = new HttpClient(new LoggingHandler(new HttpClientHandler())))
using (HttpClient client = new HttpClient(handler))
{
HttpRequestMessage request = new HttpRequestMessage();
HttpResponseMessage response = new HttpResponseMessage();
response.EnsureSuccessStatusCode();
client.BaseAddress = new Uri("https://xmltestapi.propay.com/ProtectPay/HostedTransactions/");
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string responseBody;
CreateHostedTransactionRequest transactionRequest = new CreateHostedTransactionRequest();
CreateHostedTransactionResponse transactionResponse = new CreateHostedTransactionResponse();
HttpContent content = new StringContent(transactionRequest.ToString());
try
{
request.Headers.Add("Authorization", credentials);
request.Headers.Add("accept", "application/json");
request.Method = HttpMethod.Put;
response = await client.PutAsync("https://xmltestapi.propay.com/ProtectPay/HostedTransactions/", content);
responseBody = await response.Content.ReadAsStringAsync();
objEventLog.AddLog("Transaction completed", portalSettings, userId,
response.ToString(), EventLogController.EventLogType.ADMIN_ALERT);
Console.WriteLine(responseBody);
}
catch (HttpRequestException ex)
{
result.Succeeded = false;
result.ResultMessage = ex.Message;
objEventLog.AddLog("Transaction Request Error", portalSettings, userId,
ex.Message,
EventLogController.EventLogType.ADMIN_ALERT);
Console.WriteLine("\nException Caught!");
Console.WriteLine("Message :{0} ", ex.Message);
}
return response.Content.ToString();
It seems like using System.Net.Http.HttpClient may not be the best fit due to the mismatch in TLS versions. However, before I embark on another approach (calling the API from a .NET Core web service and sending the responses back to the DNN database), I wanted to obtain input as to whether there are other issues preventing a successful connection. Thanks much for your help and guidance.