The following code for me is throwing an exception when I get a 400 error. Thus I'm never able to get return response as the exception is always caught.
using (var client = new HttpClient())
{
try
{
var data = "...";
var RequestBody = JsonConvert.SerializeObject(data);
var requestUri = new Uri("url to error");
var content = new StringContent(RequestBody, Encoding.UTF8, "application/json");
var response = client.PostAsync(requestUri, content).Result;
if (!response.IsSuccessStatusCode) // it never gets in here since an exception is thrown
{
throw new HttpException(500, "PhantomJS status request failed");
}
}
catch (Exception ex)
{
throw new Exception("PhantomJS status request failed", ex);
}
}