private static bool PostToService(FailSubmissionDto failSubmission)
{
if (!failSubmission.FailOccurrences.Any())
{
return true;
}
var postRequest = WebRequest.Create(UrlLookup.ReportFailUrl);
postRequest.Method = "POST";
postRequest.ContentType = "application/json; charset=utf-8'";
postRequest.Headers.Add("x-appfail-version", UrlLookup.ReportApiVersion);
var postData = _javaScriptSerializer.Serialize(failSubmission);
var encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postData);
postRequest.ContentLength = postData.Length;
try
{
using (var requestStream = postRequest.GetRequestStream())
{
requestStream.Write(data, 0, data.Length);
}
//We need to be careful here - if the remote server returned an error: (500) Internal Server Error
try
{
using (var responseStream = postRequest.GetResponse().GetResponseStream())
{
Debug.Assert(responseStream != null, "responseStream != null");
using (var reader = new StreamReader(responseStream))
{
var result = reader.ReadToEnd();
}
}
}
catch (WebException webException)
{
Debug.WriteLine(String.Format("Could not connect to the API - {0}", webException.Message));
}
catch(Exception exception)
{
Debug.WriteLine(String.Format("An exception occured - {0}", exception.Message));
}
return true;
}
catch (IOException)
{
}
catch (WebException)
{
}
return false;
}
}