Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

PostAsync is not working properly, whenever executed it gives exception in the catch.

$
0
0

Hi guys,
I'm having problem executing "PostAsync" as it is giving unusual error, My whole code works fine except the line with "PostAsync". The weird thing is that , it posts the content to the database through PostAsync successfully but then jumps to "Catch" and throws exception. I have given my code below, what am I doing wrong..?

FYI: I'm using Xamarin.Forms and the database is Sharepoint list

try
{
using (var client = new HttpClient()
{ Timeout = TimeSpan.FromSeconds(5) })
using (var content = new MultipartFormDataContent())
{
var values = new[]
{
new KeyValuePair<string, string>("PinCode", pincode),
new KeyValuePair<string, string>("Email", email),
new KeyValuePair<string, string>("FirstName", firstname),
new KeyValuePair<string, string>("MiddleName", middlename),
new KeyValuePair<string, string>("FamilyName", familyname)

                       };

                    foreach (var keyValuePair in values)
                        content.Add(new StringContent(keyValuePair.Value), keyValuePair.Key);

                   using (var result = await client.PostAsync(requestUri, content).ConfigureAwait(false))  //App Jumps to Catch and don't execute further
                       {
                            var input = await result.Content.ReadAsStringAsync().ConfigureAwait(false);

                            if (result.IsSuccessStatusCode)
                            {
                                await DisplayAlert("Response:>", result.StatusCode.ToString(), "OK");
                                submit.IsEnabled = false;
                            }
                            else await DisplayAlert("Response:>", "Unsuccessful", "OK");
                       }
                }
        }
        catch (TaskCanceledException ex)
        {
            if (ex.CancellationToken.IsCancellationRequested == false)
            {  await DisplayAlert("ex", "Unsuccessful", "OK");   }
            else
            { await DisplayAlert("ex", "true~~~~", "OK");  }

}


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>