As mentioned by others, it is not a good practice to get a StatusCode from an HttpRequestException, the same thing can be done in advance with an HttpResponseMessage.StatusCode after checking the HttpResponseMessage.IsSuccessStatusCode
Anyway, if due to some restriction / requirement you need to read the StatusCode, there may be two solutions
- Advanced HttpResponseMessage with an explanation of your custom exception here
- Hack an HttpRequestException.ToString to get a StatusCode. Because the message is a persistent message, fixed by StatusCode and Repharse.
Below is the code in System.Net.Http.HttpResponseMessage Where SR.net_http_message_not_success_statuscode = "The response status code does not indicate success: {0} ({1})."
public HttpResponseMessage EnsureSuccessStatusCode() { if (!this.IsSuccessStatusCode) { if (this.content != null) { this.content.Dispose(); } throw new HttpRequestException(string.Format(CultureInfo.InvariantCulture, SR.net_http_message_not_success_statuscode, new object[] { (int)this.statusCode, this.ReasonPhrase })); } return this; }
Surender Singh Malik Nov 17 '15 at 17:13 2015-11-17 17:13
source share