(note: I really really hope that I am dead wrong with this. Please let me know if you know a better solution…)
Don’t ask me why, but for some reason the server side of the PDA application I am currently developing doesn’t return the HTTP/1.1 status code in a normal way. I guess that doing that would be just too easy, you know, as I would have HttpWebResponse.StatusCode and HttpWebResponse.StatusDescription in my disposal and that’s it.
No. It always returns 200 (OK) and actual HTTP status code in header. Don’t ask me why. People, who should be smarter and more experienced then I, developed this…
But I needed to get the status message – to display it to the user, so they would understand what is wrong, not only cryptic numbers. So, I thought – that isn’t a problem. There is an enum HttpStatusCode – I’ll just pull the matching name from there and show that, “Forbidden” is at least somewhat more informative then “403”.
Enums are easy and powerful in full .NET Framework. You have all these nice methods:
|CompareTo||Compares this instance to a specified object and returns an indication of their relative values.|
|Equals||Overloaded. Overridden. Returns a value indicating whether this instance is equal to a specified object.|
|Format||Converts the specified value of a specified enumerated type to its equivalent string representation according to the specified format.|
|GetHashCode||Overridden. Returns the hash code for the value of this instance.|
|GetName||Retrieves the name of the constant in the specified enumeration that has the specified value.|
|GetNames||Retrieves an array of the names of the constants in a specified enumeration.|
|GetType||Gets the Type of the current instance. (Inherited from Object.)|
|GetTypeCode||Returns the underlying TypeCode for this instance.|
|GetUnderlyingType||Returns the underlying type of the specified enumeration.|
|GetValues||Retrieves an array of the values of the constants in a specified enumeration.|
|IsDefined||Returns an indication whether a constant with a specified value exists in a specified enumeration.|
|Parse||Overloaded. Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.|
|ReferenceEquals||Determines whether the specified Object instances are the same instance. (Inherited from Object.)|
|ToObject||Overloaded. Returns an instance of the specified enumeration type set to the specified value.|
|ToString||Overloaded. Overridden. Converts the value of this instance to its equivalent string representation.|
But as you look at the table, you’ll see that not all methods are available for Compact Framework… and if you look further, you’ll notice that out of all actually useful Enum methods, only
Enum.Parse() is supported on CF. I understand that CF needs to be tiny, but could you at least leave us couple of badly needed things?
So, long story short, I ended up using System.Reflection to get the name of matching HTTP status code. I will put the code here in hopes that I nor anyone else will never need it – neither for getting HTTP status message nor name from any enum.