0
votes

An error occurred while sending the request.===22/02/2021 09:14:31 ------------------------------Stack Trace-------------------------------------------- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at SingPost.NMP.Client.Internal.Areas.PPP.Controllers.TransactionController.d__1fd.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.b__36(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.b__3d() at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.b__3f() at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass33.b__32(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<>c__DisplayClass2b.b__1c() at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.b__1e(IAsyncResult asyncResult)===22/02/2021 09:14:31

I am getting above error message while call the API from my controller. Controller code

[HttpGet]
        public async Task<ActionResult> ToEnableDisableARandRABasedonRateScheme(long mailSchemeId)
        {
            bool updated = false;
            var headerSaveUri = string.Format("{0}/{1}/{2}", ClientConstant.WEBAPI_URI_PPI_TRANSACTION, APIMETOD_TOENABLEDISABLEARANDRABASEDONRATESCHEME, mailSchemeId);
            var client = GetHTTPClient(headerSaveUri);
            HttpResponseMessage responseMessage = await client.GetAsync(headerSaveUri);
            if (responseMessage.IsSuccessStatusCode)
            {
                var responseData = responseMessage.Content.ReadAsStringAsync().Result;
                updated = JsonConvert.DeserializeObject<bool>(responseData);
            }
            return Json(updated, JsonRequestBehavior.AllowGet);
        }

HTTP code

public HttpClient GetHTTPClient(string uri)
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(uri);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(ClientConstant.HTTP_TYPE));
            return client;
        }

In global.asax in application start event i added the below code also

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
2
It would be helpful if you can share the exception message.Arib Yousuf

2 Answers

0
votes

Controller:

public HttpClient GetHTTPClient(string uri)
{
    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri(uri);
    client.DefaultRequestHeaders.Accept.Clear();
    //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(ClientConstant.HTTP_TYPE));
    return client;
}

[HttpGet]
public async Task<ActionResult> ToEnableDisableARandRABasedonRateScheme(long? mailSchemeId)
{
    bool updated = false;
    var headerSaveUri = "http://localhost:26272/";//string.Format("{0}/{1}/{2}", ClientConstant.WEBAPI_URI_PPI_TRANSACTION, APIMETOD_TOENABLEDISABLEARANDRABASEDONRATESCHEME, mailSchemeId);
    var client = GetHTTPClient(headerSaveUri);
    var uri = $"api/Values/GetTheValue/123";
    HttpResponseMessage responseMessage = await client.GetAsync(uri);
    if (responseMessage.IsSuccessStatusCode)
    {
        var responseData = responseMessage.Content.ReadAsStringAsync().Result;
        //https://stackguides.com/questions/22838302/deserialize-json-to-c-sharp-bool
        //string json = @"{""valid"":true}";

        var jo = JObject.Parse(responseData);
        bool flag = jo.SelectToken("updated").Value<bool>();
        updated = flag;
    }
    return Json(updated, JsonRequestBehavior.AllowGet);
}

Web.API

[Route("api/Values/GetTheValue/{partner}")]
public PassMeClass GetTheValue(string partner)
{
    Request = new HttpRequestMessage();
    Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration());

    Configuration = new HttpConfiguration();

    var theClinic = new PassMeClass();
    theClinic.updated = true;

    return theClinic;
}

public class PassMeClass
{
    public bool updated { get; set; }
}

property of Web.API

0
votes

Try changing actionresult to jsonresult instead in the following function:

     [HttpGet]
    public async Task<JsonResult> ToEnableDisableARandRABasedonRateScheme(long mailSchemeId)
    {
        bool updated = false;
        var headerSaveUri = string.Format("{0}/{1}/{2}", ClientConstant.WEBAPI_URI_PPI_TRANSACTION, APIMETOD_TOENABLEDISABLEARANDRABASEDONRATESCHEME, mailSchemeId);
        var client = GetHTTPClient(headerSaveUri);
        HttpResponseMessage responseMessage = await client.GetAsync(headerSaveUri);
        if (responseMessage.IsSuccessStatusCode)
        {
            var responseData = responseMessage.Content.ReadAsStringAsync().Result;
            updated = JsonConvert.DeserializeObject<bool>(responseData);
        }
        return Json(updated, JsonRequestBehavior.AllowGet);
    }