Httpstatuscode does not exist code 207, 208 and 226

Asked

Viewed 103 times

5

What to do when class HttpStatusCode there is no code 207, 208 and 226 ?

Someone already asked the question on Github: https://github.com/dotnet/corefx/issues/4382, I don’t think they did an "update". Follows code:

HttpResponseMessage response = await httpClient.PostAsync();
if ((int)response.StatusCode < 200 || (int)response.StatusCode > 226 && (int)response.StatusCode != 404) //[200,208] = HTTP OK
{
    //HTTP Response Not OK
}

In the above code you can only enter the if when it is different from the list below.

Here is the list of the family "Success":

  1. 200
  2. 201
  3. 202
  4. 203
  5. 204
  6. 205
  7. 206
  8. 207
  9. 208
  10. 226

List source: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#Respostas_de_sucesso

  • What is your doubt? if Httpstatuscode does not exist or how to validate it ? your question has become very vague

  • @Marcosbrinner If code comes 207, will generate the right exception ? How can I treat this ?

  • @Marcosbrinner can only enter the if different from 200 to 226.

  • What exactly is happening and what you are trying to do?

  • @Leandroangelo I am consuming API that can return the 3 codes above (207, 208 and 226). Look at my logic, which is saying that can only enter the if different from 200 to 226, but there is no such 3 codes, what to do ?

  • I mean, you can only enter if when it was not of the family "Success" !

Show 1 more comment

2 answers

4


Successful not only the range you present, it goes from 200 to 299, you can validate through the property IsSuccessStatusCode class HttpResponseMessage.

HttpResponseMessage response = await httpClient.PostAsync();
if (!response.IsSuccessStatusCode)
{
    //HTTP Response Not OK
}

Reference

  • Your problem is with StatusCode with the asynchronous call treatment, you get a 207 as a response?

0

You can also use the library System Net., it contains an enumerator called Httpstatuscode and you can compare the results instead of using magic numbers.

HttpResponseMessage response = await httpClient.PostAsync();
if ((int)response.StatusCode < (int)HttpStatusCode.NotFound)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.