I using HttpClient to send post request on local server (powered on LAMP or MAMP, tried both) but can't get answer, always getting "Task cancelled exception" with following code
using (HttpClient client = new HttpClient())
client.Timeout = new TimeSpan(0, 0, 10);
var sendContent = new StringContent(serialized);
using (HttpResponseMessage response = await client.PostAsync(url.ToString(), sendContent))
if (response.StatusCode != HttpStatusCode.OK)
return MakeError("Bad status: " + response.StatusCode.ToString());
using (HttpContent content = response.Content)
string str = await content.ReadAsStringAsync();
if (str == null)
return MakeError("Got null answer");
App.Log("Response: " + str);
catch (Exception e)
App.Log("There is something bad with request: " + serialized + " the error was " + e.Message + " url = " + url.ToString());
return MakeError("Timed out");
One possible issue is your 8 second timeout.
From HttpClient documentation:
The default value is 100,000 milliseconds (100 seconds).
A Domain Name System (DNS) query may take up to 15 seconds to return or time out. If your request contains a host name that requires resolution and you set Timeout to a value less than 15 seconds, it may take 15 seconds or more before a WebException is thrown to indicate a timeout on your request.
So 15 seconds at least are needed for the DNS query if that is taking place in your call. Remove the timeout and see if the issue is still there.