public static void EnsureSuccess(this HttpWebResponse response) { if (!response.IsSuccessful()) { throw new WebException("Request unsuccessful", null, WebExceptionStatus.ReceiveFailure, response); } }
private static string GetResponseMessage(HttpWebResponse response) { if (response.IsSuccessful()) return null; int statusCode = (int)response.StatusCode; string responseText = response.GetResponseText(); string message = statusCode == 404 ? "404 Page not found." : responseText.Length < 500 ? responseText : ""; if (responseText.Trim().StartsWith("{")) { try { var responseJson = JObject.Parse(responseText); message = responseJson["message"].Value<string>(); } catch { } } return message; }