Forum Xamarin.Forms

HttpClient PostAsync doesn't return anything

SteveRussellSteveRussell Member ✭✭✭
edited February 5 in Xamarin.Forms

I have a PostAsync that doesn't return anything and doesn't continue code execution afterwards. I get no error but nothing happens when it reaches PostAsync. My code:

        public async System.Threading.Tasks.Task<HttpResponseMessage> LoginAsync(string name, string name2, int sourceid)
        {
            var client = InitiateHttpClient("");
            var json = JsonConvert.SerializeObject(new
            {
                name = name,
                name2 = name2,
                sourceid = sourceid,
            });
            var data = new StringContent(json, Encoding.UTF8, "application/json");
            var response = await client.PostAsync("myRequestUri", data).ConfigureAwait(false);
            return response;
        }

            HttpResponseMessage httpResponseMessage = await login.LoginAsync(Text1, Text2, Source);

EDIT:

After several seconds (possibly 1-2 minutes) I get Exception Unhandled
System.Threading.Tasks.TaskCanceledException: 'The operation was canceled.'

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    Try to check your web service with Postman. Will it get the expected response when using Postman to send the same post request?
    If your InitiateHttpClient has instantiated a correct HttpClient then your code should work if the parameters match with the backend service.
    So we could use Postman to test the connection of the backend service and we also need to check the validation of the parameters in Xamarin.

Sign In or Register to comment.