Before doing anything else with your code, such as adding disposal, let me throw away unused variables, use a better way to access the exception and move variable declarations:
using System.Net;
using System.IO;
...
System.Exception ex;
try
{
WebHeaderCollection httpHeaders = new WebHeaderCollection();
httpHeaders.Add("Authorization", "acess_token" WTL_6DeeAPITokenSerice::getToken());
httpHeaders.Add("requestid", "23556667772");
httpHeaders.Add("sourcenode", "ERP");
httpHeaders.Add("requesttimestamp", "2022-09-12");
HttpWebRequest request = System.Net.WebRequest::Create("https://API URL");
request.set_Headers(httpHeaders);
request.Method = "POST";
request.ContentType = "application/json";
StreamWriter streamWriter = new StreamWriter(request.GetRequestStream());
str sJSON = 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);
System.Text.Encoding encoding = System.Text.Encoding::get_ASCII();
System.Byte[] postBytes = encoding.GetBytes(sJSON);
streamWriter.Write(postBytes); // writing JSON
streamWriter.Flush();
streamWriter.Close();
System.Net.HttpWebResponse response = request.GetResponse();
StreamReader streamRead = new StreamReader(response.GetResponseStream());
info(streamRead.ReadToEnd());
}
catch (ex)
{
throw error(ex.Message);
}
What you do with ASCII encoding looks strange to me; let me throw it away. Also, you now have a bug in the header - you replaced Bearer with acess_token, whicdh is wrong, and you forgot to add the space. Let me fix it too.
using System.Net;
using System.IO;
...
System.Exception ex;
try
{
WebHeaderCollection httpHeaders = new WebHeaderCollection();
httpHeaders.Add("Authorization", "Bearer " WTL_6DeeAPITokenSerice::getToken());
httpHeaders.Add("requestid", "23556667772");
httpHeaders.Add("sourcenode", "ERP");
httpHeaders.Add("requesttimestamp", "2022-09-12");
HttpWebRequest request = System.Net.WebRequest::Create("https://API URL");
request.Headers = httpHeaders;
request.Method = "POST";
request.ContentType = "application/json";
StreamWriter streamWriter = new StreamWriter(request.GetRequestStream());
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);
streamWriter.Write(json); // writing JSON
streamWriter.Flush();
streamWriter.Close();
System.Net.HttpWebResponse response = request.GetResponse();
StreamReader streamRead = new StreamReader(response.GetResponseStream());
info(streamRead.ReadToEnd());
}
catch (ex)
{
throw error(ex.Message);
}
Your code calls Close(), which is good, but it does only everything completes successfully. If it doesn't, you keep the writer open. You can utilize the 'using' statement to do it in both cases. And let's do it with the response too.
using System.Net;
using System.IO;
...
System.Exception ex;
try
{
WebHeaderCollection httpHeaders = new WebHeaderCollection();
httpHeaders.Add("Authorization", "Bearer " WTL_6DeeAPITokenSerice::getToken());
httpHeaders.Add("requestid", "23556667772");
httpHeaders.Add("sourcenode", "ERP");
httpHeaders.Add("requesttimestamp", "2022-09-12");
HttpWebRequest request = System.Net.WebRequest::Create("https://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()))
{
streamWriter.Write(json); // writing JSON
using (System.Net.HttpWebResponse response = request.GetResponse())
{
StreamReader streamRead = new StreamReader(response.GetResponseStream());
info(streamRead.ReadToEnd());
}
}
}
catch (ex)
{
throw error(ex.Message);
}