We use HttpClient to publish json for a relaxed web service. In one case, we are faced with something that puzzled us. Using tools such as the postman, script, etc., we can publish it to the end point and see that it works. When we do the same with HttpClient.PostAsJsonAsync, we can check in the software that we send that it received the data just fine. However, our PostAsJsonAsync will always end up with a timeout and not give us an answer.
We worked with the team that created the service we consume, plus our additional testing on our side, and we have not yet been able to truly time out this service.
Every time we make a message with the HttpClient, we can verify that the target software that we publish is actually receiving data. Each time we send a message to this target software from any other tool, we always very quickly see a response with a status code of 200. Something about HttpClient does not accept a response from this particular service. Does anyone have an idea what we can look at here?
Here's the code (although it's such a cookie cutter that I almost don't feel it necessary)
public string PostData(string resourcePath, Object o, Boolean isCompleteUrl = false, int timeoutMinutes = -1) { using (var client = new HttpClient()) { if (timeoutMinutes > 0) { client.Timeout = new TimeSpan(0,timeoutMinutes,0); } var useUrl = isCompleteUrl ? resourcePath : ApiBase + resourcePath; var response = client.PostAsJsonAsync(useUrl, o).Result; if(response.StatusCode == System.Net.HttpStatusCode.OK) { return response.Content.ReadAsStringAsync().Result; } return ""; } }
Danny
source share