HI All,
I am facing an issue while calling sending a request to API.
Below is the error message.
Microsoft.Dynamics.Ax.Xpp.ErrorException: 'The request was aborted: The connection was closed unexpectedly.'
using Newtonsoft.Json.Linq;
using System.Net;
using System.IO;
public class WTL_6DeePaymentAPIservice
{
public static void postPaymentAPI()
{
str _profileGUID = "3ace35c3-6443-eb11-a812-000d3a20f116";
str _transactionID = "12323553434";
str _amount = "25.00";
WTL_PaymentMode _paymentMode = "1";
str _accountID = "1000107754";
WTL_TransactionCurrencyID _currencyCode = "QAR";
WTL_AccountName _customerName = "Harryson Jerico";
str _amountPaid = "25";
str _externalID = "4520941365";
str _cardHolderName = "Patrisia Allen";
str _modelValue = "113cvxrdu1";
str _modelKey = "sample_key";
str _paymentGWtoken = "vsE0aZWzPq02376";
str _comment = "order payment";
str _authToken = "xrzy6ryymust";
str _orderType = "MakePayment";
str _id = "";
str _value = "";
str _contactNumber = "(555) 555-1234";
System.Exception ex;
try
{
JObject jObject = JObject::parse(WTL_6DeeAPITokenSerice::getToken());
WebHeaderCollection httpHeaders = new WebHeaderCollection();
httpHeaders.Add("Authorization", "Bearer " strFmt("%1",jObject.SelectToken("$.access_token")));
httpHeaders.Add("requestid", "7777777");
httpHeaders.Add("sourcenode", "ERP");
httpHeaders.Add("requesttimestamp", "2022-09-12");
HttpWebRequest request = System.Net.WebRequest::Create("API URL");
request.Headers = httpHeaders;
request.Method = "POST";
request.ContentType = "application/json";
str json = WTL_6DJSONserializer::serializeJSON(
_profileGUID,
_transactionID,
strFmt("%1",_amount),
_paymentMode,
_accountID,
_currencyCode,
_customerName,
strFmt("%1",_amountPaid),
_externalID,
_cardHolderName,
_modelValue,
_modelKey,
_paymentGWtoken,
_comment,
_authToken,
_orderType,
_id,
_value,
_contactNumber);
using (StreamWriter streamWriter = new StreamWriter(request.GetRequestStream()))
{
System.Byte[] postBytes;
System.Text.Encoding encoding = System.Text.Encoding::get_ASCII();
postBytes = encoding.GetBytes(json);
streamWriter.Write(postBytes); // writing JSON
using (System.Net.HttpWebResponse response = request.GetResponse())
{
StreamReader streamRead = new StreamReader(response.GetResponseStream());
info(streamRead.ReadToEnd());
}
}
}
catch (ex)
{
throw error(ex.Message);
}
//catch (Exception::CLRError)
//{
// throw error(AifUtil::getClrErrorMessage());
//}
}
}
I have pasted my whole code.
And while debugging I have found that the exception is thrown at " using (StreamWriter streamWriter = new StreamWriter(request.GetRequestStream())) "
Please help me to understand this issue and fix it