Connection reset issue in .net core api call (net::ERR_CONNECTION_RESET 200 (OK))

Nidhin Paul 1 Reputation point
2021-11-05T11:20:03.747+00:00

I have a .net core API service which is called from a angular client project.

When a user request a status of his payment, we will make call to this service api and this service will then call a payment gateway service to fetch the status of payment and the output result will return to the user.

When i try to integrate this i am facing this below error.

net::ERR_CONNECTION_RESET 200 (OK)
core.js:5967 ERROR Unknown Error

This above issue is not showing when i try to hit the service after putting one breakpoint. Its also returning the result.

This is how entire flow works

Client side call performs by user

this.dataservice.postFeed(method, JSON.stringify(this.initsearch)).subscribe(result => {
var response = result.body["data"];
console.log(response);
});

[HttpPost]
[ProducesResponseType(typeof(int), 401)]
[ProducesResponseType(typeof(int), 400)]
[ProducesResponseType(typeof(string), 500)]
public async Task<IActionResult> Post([FromBody] PaymentSearchModel searchValue)
{
try
{

           ApiResponse<string> response = new ApiResponse<string>();

           if (searchValue != null)
           {
               searchValue.searchcode = "1";//setting for the time being for auditing purpose
               string result = await _adlerBo.GetPaymentStatus(searchValue);

               response.Success = true;
               response.Data = result;

               return Ok(response);
           }
           else
           {
               return StatusCode(StatusCodes.Status400BadRequest, "No required values submitted");
           }
       }
       catch (Exception ex)
       {
           return StatusCode(StatusCodes.Status500InternalServerError, "There was a problem in retreiving the data");
       }
   }

private async Task<string> GetPaymentStatus(PaymentSearchModel requestModel)
{
string result = string.Empty;

       try
       {
           AdlerCCAvenue adlerCCAvenue = new AdlerCCAvenue();

           StatusAPIResponseModel objResponseModel = new StatusAPIResponseModel();

           //switch (requestModel.searchcode) {
           //    case "1"://search using order  id
           //        adlerCCAvenue = await _adlerDao.GetAvenuePaymentDetails(requestModel.searchcode,requestModel.orderid);
           //        break;
           //}

           //START Check Payment Status
           //if (adlerCCAvenue != null && adlerCCAvenue.PaymentId > -1)
           //{
           string apiResponse = await PaymentStatusCheckUsingAPI(requestModel.orderid);

           await Task.Delay(10000);

           if (!string.IsNullOrEmpty(apiResponse))
           {
               result = apiResponse;
           }
           //}
           //END
       }
       catch (Exception ex)
       {
           _appLogger.CreateLog("GetPaymentStatus - " + ex);
       }

       return result;
   }

private async Task<string> PaymentStatusCheckUsingAPI(string orderNumber)
{
string resJson = string.Empty;

    try
    {
        string accessCode = _configuration["Avenue:accessCode"];
        string workingKey = _configuration["Avenue:workingkey"];
        string statusApiUrl = _configuration["Avenue:status_api_url"];

        //string orderStatusQueryJson = "{ \"reference_no\":\"\", \"order_no\":\"" + orderNumber + "\" }"; //Ex. { "reference_no":"CCAvenue_Reference_No" , "order_no":"123456"}
        string orderStatusQueryJson = "{ \"order_no\":\"" + orderNumber + "\" }"; //Ex. { "reference_no":"CCAvenue_Reference_No" , "order_no":"123456"}
        string encJson = "";

        CCACrypto ccaCrypto = new CCACrypto();
        encJson = ccaCrypto.Encrypt(orderStatusQueryJson, workingKey);

        // make query for the status of the order to ccAvenues change the command param as per your need
        ////string authQueryUrlParam = "enc_request=" + encJson + "&access_code=" + accessCode + "&command=orderStatusTracker&request_type=JSON&response_type=JSON";

        // Url Connection
        ////string message = PostPaymentRequestToGateway(statusApiUrl, authQueryUrlParam);
        string message = await PostPaymentRequestToGateway(statusApiUrl, encJson, accessCode);

        NameValueCollection param = GetResponseMap(message);

        string status = "";
        string encResJson = "";
        if (param != null && param.Count == 2)
        {
            for (int i = 0; i < param.Count; i++)
            {
                if ("status".Equals(param.Keys[i]))
                {
                    status = param[i];
                }
                if ("enc_response".Equals(param.Keys[i]))
                {
                    encResJson = param[i];
                }
            }
            if (!"".Equals(status) && status.Equals("0"))
            {
                resJson = ccaCrypto.Decrypt(encResJson, workingKey);
            }
            else if (!"".Equals(status) && status.Equals("1"))
            {
                //Console.WriteLine("failure response from ccAvenues: " + encResJson);
            }
        }
    }
    catch (Exception exp)
    {
        _appLogger.CreateLog("PaymentStatusCheckUsingAPI - " + exp);
    }

    return resJson;
}

private async Task<string> PostPaymentRequestToGateway(string queryUrl, string urlParam,string accessCode)
{
string message = "";
try
{
using (HttpClient client = new HttpClient())
{
var parameters = new Dictionary<string, string> { { "enc_request", urlParam },
{ "access_code", accessCode },
{ "command", "orderStatusTracker" },{"request_type", "JSON" },{"response_type", "JSON" }};

             var encodedContent = new FormUrlEncodedContent(parameters);

             HttpResponseMessage response = await client.PostAsync(queryUrl, encodedContent);
             response.EnsureSuccessStatusCode();
             string responseBody = await response.Content.ReadAsStringAsync();
             message = responseBody;
         }






         //    StreamWriter myWriter = null;// it will open a http connection with provided url
         //WebRequest objRequest = WebRequest.Create(queryUrl);//send data using objxmlhttp object
         //objRequest.Method = "POST";
         ////objRequest.ContentLength = TranRequest.Length;
         //objRequest.ContentType = "application/x-www-form-urlencoded";//to set content type
         //myWriter = new System.IO.StreamWriter(objRequest.GetRequestStream());
         //myWriter.Write(urlParam);//send data
         //myWriter.Close();//closed the myWriter object

         //// Getting Response
         //System.Net.HttpWebResponse objResponse = (System.Net.HttpWebResponse)objRequest.GetResponse();//receive the responce from objxmlhttp object
         //using (System.IO.StreamReader sr = new System.IO.StreamReader(objResponse.GetResponseStream()))
         //{
         //    message = sr.ReadToEnd();
         //}
     }
     catch (Exception exception)
     {
         Console.Write("Exception occured while connection." + exception);
     }
     return message;

 }

private NameValueCollection GetResponseMap(string message)
{
//await Task.Delay(2000);

       NameValueCollection Params = new NameValueCollection();
       if (message != null || !"".Equals(message))
       {
           string[] segments = message.Split('&');
           foreach (string seg in segments)
           {
               string[] parts = seg.Split('=');
               if (parts.Length > 0)
               {
                   string Key = parts[0].Trim();
                   string Value = parts[1].Trim();
                   Params.Add(Key, Value);
               }
           }
       }
       return Params;
   }
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,545 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,887 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.