Nithin Paul
Nithin Paul

Reputation: 2199

Connection reset issue in ASP.NET Core Web API call (net::ERR_CONNECTION_RESET 200 (OK))

I have an ASP.NET Core Web API service which is called from an 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 get this 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. It's also returning the result.

This is how entire flow works

  1. Client side call performs by user

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

    [HttpPost]
    public async Task<IActionResult> Post([FromBody] ObjectModel searchValue)
    {
          ApiResponse<string> response = new ApiResponse<string>();
          IBaseResult<string> result = await _adlerBo.GetPaymentStatus(searchValue);
    
          response.Success = result.success;
          response.Data = result.Data;
    
          return Ok(response);
    }
    

In BusinessObject.cs:

   public async  Task<IBaseResult<string>> GetPaymentStatus(PaymentSearchModel requestModel){
          string apiResponse = await PaymentStatusCheckUsingAPI(requestModel.orderid);
          return apiResponse ;
   }

   private async Task<string> PaymentStatusCheckUsingAPI(string orderNumber)
   {
         string message = await PostPaymentRequestToGateway(statusApiUrl, authQueryUrlParam);
         NameValueCollection param = await 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 = crypto.Decrypt(encResJson, workingKey);
                }
                else if (!"".Equals(status) && status.Equals("1"))
                {
                     Console.WriteLine("failure response: " + encResJson);
                }
       }

       return resJson;
   }

   private async Task<string> PostPaymentRequestToGateway(string queryUrl, string urlParam)
   {
        string message = "";

        try
        {
            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 = await sr.ReadToEndAsync();
                //Response.Write(message);
            }
        }
        catch (Exception exception)
        {
            Console.Write("Exception occurred while connection." + exception);
        }

        return message;
    }

    private async Task<NameValueCollection> GetResponseMap(string message)
    {
        //await Task.Delay(2000); I did this with no Luck

        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 await Task.FromResult(Params);
    }

Any idea how to fix this? Why its working when I put breakpoint and not otherwise.

Am I doing correct asynchronous implementation in my API ?

Upvotes: 0

Views: 897

Answers (0)

Related Questions