Reputation: 11
I'm writing a WP8.1 app that reads and sends data through REST API. All of them work without problems excepts the "search" one.
This API works in POST and I need to send a string in Json format defining my query. If I set only few filters, the REST API takes too long to respond. Therefore the HttpResponseMessage object has no success code and it returns a "NotFound" message, even if the web API is correcly runnning.
If I try the same request via Postman it works correctly, but if I try it via SoapUi I get a "java.net.SocketTimeoutException: Read timed out" error message.
In SoapUi I managed to extend timeout in order to get a response, but my C# code keeps on not working even if I set the TimeOut property to HttpClient object.
Can someone please help me to resolve my problem? Thanks!
EDIT The code is really simple:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(baseAddress);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.ExpectContinue = false;
client.Timeout = TimeSpan.FromSeconds(300);
HttpContent content = new StringContent(json, Encoding.Unicode, "application/json");
HttpResponseMessage response = await client.PostAsync(uri, content));
if (response.IsSuccessStatusCode)
{
string jsonResponse = await response.Content.ReadAsStringAsync();
}
Upvotes: 0
Views: 1418
Reputation: 1101
I know that this may won't like you, but have you tried with RestSharp? For some reason in this particular scenario when I need to send information via POST, HTTPClient doesn't work as expected and the "content" information it's set as part of the uri instead of the body.
The equivalente block of code might be like this:
var client = new RestClient("http://example.com");
var request = new RestRequest("resource/{id}", Method.POST);
request.AddParameter("name", "value");
// execute the request
IRestResponse response = client.Execute(request);
client.ExecuteAsync(request, response => {
Console.WriteLine(response.Content);
});
Upvotes: 1