I am using POST to create orders into another system. My goal is to receive a strongly typed exception object, so I can log the exception message.
I just receive a message that a server error 500 has occurred. Is there a way I can get the error details?
It appears I need to receive the JSON as a string, so I can strong type it to a class of exception. When I call GetResponse(), I get an exception that does not contain the error details.
Here is part of my code:
public string CreateOrder(Order order, bool deleteExistingOrder) {
try {
string postString = JsonConvert.SerializeObject(order);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(m_Url + "/Orders/CreateOrder");
req.Timeout = 90000;
req.Method = "POST";
req.Accept = "application/json";
req.ContentType = "application/json";
string userP = m_UserName + ":" + m_Password;
byte[] authBytes = Encoding.UTF8.GetBytes(userP).ToArray();
req.ContentLength = Encoding.UTF8.GetByteCount(postString);
using (Stream sw = req.GetRequestStream()) {
byte[] bytes = Encoding.UTF8.GetBytes(postString);
sw.Write(bytes, 0, bytes.Length);
}
HttpWebResponse resp = null;
resp = (HttpWebResponse)req.GetResponse();
// Verify HTTP for 200 OK status code
if (resp.StatusCode == HttpStatusCode.OK) {
using (StreamReader sr = new StreamReader(resp.GetResponseStream(), Encoding.UTF8)) {
string responseData = sr.ReadToEnd().Replace("\r\n", "");
}
}
return "OK"
} catch (Exception ex) {
return String.Format("Error inserting order {0}. {1}", order.orderNumber, ex.ToString());
}
Here is the response:
{ "Message":"An error has occurred.","ExceptionMessage":"Invalid storeId","ExceptionType":"System.ArgumentException","StackTrace":" at