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
try
{
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);
return 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");
}
the url is right, if I trying to execute this code on C# Console Application I can get answer (but not with Xamarin, both Android and iOS, on device and emulators).
I also tried to sniff HTTP packets, and I saw that answer was sent by my local server, but Xamarin didn't handled this right way. BUT, if replace url with domain (for example http://stackoverflow.com) I can get answer.
Following HTTP answer headers:
- Connection →close (keep-alive doesn't works too)
- Content-Length →951
- Content-Type →application/json
- Date →Thu, 16 Jun 2016 18:11:40 GMT
- Server →Apache
- X-Powered-By →PHP/5.5.14
Any suggestions?