Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpClient PostAsync does not return

I've seen a lot of question about this, and all points to me using ConfigureAwait(false), but even after doing so, it still doesn't returned any response. When I run the debugger, the code stops at the PostAsync and does not continue with my code. Am I doing something wrong here? Does it have to do with me calling an API via HTTPS?

Here's the code:

public async static Task<PaymentModel> AddAsync(Card card)
{
    HttpClient client = new HttpClient();

    client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:", "hidden"))));

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

    var cardJson = JsonConvert.SerializeObject(card);
    var postRequest = new StringContent(cardJson, Encoding.UTF8, "application/json");
    var request = await client.PostAsync(new Uri("https://sample-3rd-party-api/api/endpoint/here"), postRequest).ConfigureAwait(false);
    var content = await request.Content.ReadAsStringAsync().ConfigureAwait(false);
}

EDIT:

In response to the comments below, the code is contained from a method AddAsync(Card card) called from a button click with a handler:

public async void OnExecute(object sender, EventArgs args)
{
    //some code here
    payment = await PaymentModel.AddAsync(card).ConfigureAwait(false);
}

EDIT 2: I tried pinging the API, but it returns a request timed out, but when I tried it using Postman, it's doing fine (the API is just a Sandbox which is open for all, so it's okay to share this): enter image description here

enter image description here

EDIT 3:

I think the problem lies with where I don't have an SSL certificate to access the API. I have a PHP server that connects to the same API and I have to set SSL_VERIFYPEER to false just so I can access it (don't worry, I added a cacert now so its on true again). Can the same issue be happening here? If so, what can I do to create a valid certificate for my xamarin forms app

like image 1000
Lala Avatar asked Apr 28 '18 09:04

Lala


2 Answers

You can use this

var json = JsonConvert.SerializeObject(card);
using (var client = new HttpClient())
{
    var t = await client.PostAsJsonAsync("https://sample-3rd-party-api/api/endpoint/here", json);

    Response R =JsonConvert.DeserializeObject<Response>((JsonConvert.DeserializeObject(t.Content.ReadAsStringAsync().Result.ToString())).ToString());
}
like image 130
Erfan Mohammadi Avatar answered Nov 11 '22 12:11

Erfan Mohammadi


What's most likely happening here is your OnExecute method has a return type of void instead of Task which prevents the UI thread from being able to await it. Try either changing that return type to Task or creating a new UI thread to perform this work. I wouldn't worry about the ping timing out as long as Postman works. Many public web servers disable their ping response.

like image 1
S.C. Avatar answered Nov 11 '22 12:11

S.C.