13
HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
HttpStatusCode statusCode = response.StatusCode;

In this code statusCode.ToString() returns for example "BadRequest" but I need "Bad Request"

I saw arcticles about response.ReasonPhrase, but that's not what I need and it is not supported by HttpWebResponse, only supported by HttpResponseMessage from HttpClient

Another example against Regex.Replace solution: (414) RequestUriTooLong -> Request-Uri Too Long

11
  • Just put a space before any upper-case letters that aren't at the beginning of the value.
    – rory.ap
    Commented May 29, 2018 at 15:39
  • 2
    What if the remote server returns a code of 418 though? What would you expect that to convert to?
    – DavidG
    Commented May 29, 2018 at 15:39
  • 1
    What is the problem you are trying to solve where you have decided that the solution requires "Bad Request" as opposed to "BadRequest"? Commented May 29, 2018 at 15:41
  • 2
    Did you check the HttpWebResponse.StatusDescription? Commented May 29, 2018 at 15:48
  • 1
    @Vitaliy the programs display the StatusCode and StatusDescription. Did you check it? Even if it's empty you can use something like Humanizer to convert the enum to a readable name. This won't work if the status code isn't defined in the enum though Commented May 29, 2018 at 15:50

3 Answers 3

17

Based on reference source, you can retrieve the English status description with a simple call into a static class, given the status code:

int code = 400;

/* will assign "Bad Request" to text */
var text = System.Web.HttpWorkerRequest.GetStatusDescription(code);

Texts are defined for ranges 100 - 507, returning empty strings for special codes like 418 and 506.

2
  • 1
    Isn't that already available through the StatusDescription property? Commented May 29, 2018 at 15:56
  • indeed. so it answers the question for somebody who has only got the status code, OP has the complete HttpWebResponse instance anyway. Commented May 29, 2018 at 15:59
5

HttpStatusCode is an enum which has camel-cased member names.
You can use this one-liner to accomplish your need by putting space between camel-cased:

return Regex.Replace(statusCode.ToString(), "(?<=[a-z])([A-Z])", " $1", RegexOptions.Compiled);
0

Here, I pulled this out of a string utility class I have. Might be overkill, but it's a useful thing. Use the ToTitleCase extension method.

/// <summary>
/// A dictionary that holds the collection of previous title case
/// conversions so they don't have to be done again if needed more than once.
/// </summary>
private static Dictionary<string, string> _prevTitleCaseConversions = new Dictionary<String, String>();

/// <summary>
/// A collection of English words that should be lower-case in title-cased phrases.
/// </summary>
private static List<string> _englishTitleCaseLowerCaseWords = new List<string>() {"aboard", "about", "above", "across", "after",
    "against", "along", "amid", "among", "anti", "around", "is", "as", "at", "before", "behind", "below",
    "beneath", "beside", "besides", "between", "beyond", "but", "by", "concerning", "considering",
    "despite", "down", "during", "except", "excepting", "excluding", "following", "for", "from", "in",
    "inside", "into", "like", "minus", "near", "of", "off", "on", "onto", "opposite", "outside", "over",
    "past", "per", "plus", "regarding", "round", "save", "since", "than", "through", "to", "toward",
    "towards", "under", "underneath", "unlike", "until", "up", "upon", "versus", "via", "with", "within", "without",
    "and", "but", "or", "nor", "for", "yet", "so", "although", "because", "since", "unless", "the", "a", "an"};

/// <summary>
/// Convert the provided alpha-numeric string to title case.  The string may contain spaces in addition to letters and numbers, or it can be
/// one individual lowercase, uppercase, or camel case token.
/// </summary>
/// <param name="forValue">The input string which will be converted.  The string can be a 
/// normal string with spaces or a single token in all lowercase, all uppercase, or camel case.</param>
/// <returns>A version of the input string which has had spaces inserted between each internal "word" that is 
/// delimited by an uppercase character and which has otherwise been converted to title case, i.e. all 
/// words except for conjunctions, prepositions, and articles are upper case.</returns>
public static string ToTitleCase(this string forValue)
{
    if (string.IsNullOrEmpty(forValue)) return forValue;

    if (!Regex.IsMatch(forValue, "^[A-Za-z0-9 ]+$"))
        throw new ArgumentException($@"""{forValue}"" is not a valid alpha-numeric token for this method.");

    if (_prevTitleCaseConversions.ContainsKey(forValue)) return _prevTitleCaseConversions[forValue];

    var tokenizedChars = GetTokenizedCharacterArray(forValue);
    StringBuilder wordsSB = GetTitleCasedTokens(tokenizedChars);
    string ret = wordsSB.ToString();
    _prevTitleCaseConversions.Add(forValue, ret);
    return ret;
}

/// <summary>
/// Convert the provided string such that first character is 
/// uppercase and the remaining characters are lowercase.
/// </summary>
/// <param name="forInput">The string which will have 
/// its first character converted to uppercase and 
/// subsequent characters converted to lowercase.</param>
/// <returns>The provided string with its first character 
/// converted to uppercase and subsequent characters converted to lowercase.</returns>
private static string FirstUpper(this string forInput)
{
    return Alter(forInput, new Func<string, string>((input) => input.ToUpperInvariant()), new Func<string, string>((input) => input.ToLowerInvariant()));
}

/// <summary>
/// Return an array of characters built from the provided string with 
/// spaces in between each word (token).
/// </summary>
private static ReadOnlyCollection<char> GetTokenizedCharacterArray(string fromInput)
{
    var ret = new List<char>();
    var tokenChars = fromInput.ToCharArray();
    bool isPrevCharUpper = false;
    bool isPrevPrevCharUpper = false;
    bool isPrevPrevPrevCharUpper = false;
    bool isNextCharUpper = false;
    bool isNextCharSpace = false;

    for (int i = 0; i < tokenChars.Length; i++)
    {
        char letter = tokenChars[i];
        bool addSpace;
        bool isCharUpper = char.IsUpper(letter);

        if (i == 0) addSpace = false; // no space before first char.
        else
        {
            bool isAtLastChar = i == tokenChars.Length - 1;
            isNextCharUpper = !isAtLastChar && char.IsUpper(tokenChars[i + 1]);
            isNextCharSpace = !isAtLastChar && !isNextCharUpper && tokenChars[i + 1].Equals(' ');
            bool isInAcronym = (isCharUpper && isPrevCharUpper && (isAtLastChar || isNextCharSpace || isNextCharUpper));
            addSpace = isCharUpper && !isInAcronym;
        }

        if (addSpace) ret.Add(' ');
        ret.Add(letter);
        isPrevPrevPrevCharUpper = isPrevPrevCharUpper;
        isPrevPrevCharUpper = isPrevCharUpper;
        isPrevCharUpper = isCharUpper;
    }

    return ret.AsReadOnly();
}

/// <summary>
/// Return a string builder that will produce a string which contains
/// all the tokens (words separated by spaces) in the provided collection
/// of characters and where the string conforms to title casing rules as defined above.
/// </summary>
private static StringBuilder GetTitleCasedTokens(IEnumerable<char> fromTokenChars)
{
    StringBuilder wordsSB = new StringBuilder();
    var comparer = StringComparer.Create(System.Globalization.CultureInfo.CurrentCulture, true);
    var words = new string(fromTokenChars.ToArray()).Split(' ');
    bool isFirstWord = true;

    foreach (string word in words)
    {
        if (word.Length == 0) continue;
        if (wordsSB.Length > 0) wordsSB.Append(' ');
        bool isAcronym = word.Length > 1 && word.All((c) => char.IsUpper(c));
        string toAppend;

        // leave acronyms as-is, and lower-case all title case exceptions unless it's the first word.
        if (isAcronym) toAppend = word;
        else if (isFirstWord || !_englishTitleCaseLowerCaseWords.Contains(word, comparer)) toAppend = word.FirstUpper();
        else toAppend = word.ToLower();

        wordsSB.Append(toAppend);
        isFirstWord = false;
    }

    return wordsSB;
}

/// <summary>
/// Convert the provided string such that first character is altered using 
/// <paramref name="firstCharAlterationFunction"/> and the remaining characters
/// are altered using <paramref name="remainingCharsAlterationFunction"/>.
/// </summary>
/// <param name="forInput">The string which will have 
/// its first character altered using <paramref name="firstCharAlterationFunction"/> and 
/// subsequent characters altered using <paramref name="remainingCharsAlterationFunction"/>.</param>
/// <param name="firstCharAlterationFunction">The function which will
/// be used to alter the first character of the input string.</param>
/// <param name="remainingCharsAlterationFunction">The function which
/// will be used to ever character in the string after the first character.</param>
/// <returns>The provided string with its first character 
/// altered using <paramref name="firstCharAlterationFunction"/> and 
/// subsequent characters altered using <paramref name="remainingCharsAlterationFunction"/>.</returns>
private static string Alter(string forInput, Func<string, string> firstCharAlterationFunction, Func<string, string> remainingCharsAlterationFunction)
{
    if (string.IsNullOrWhiteSpace(forInput)) return forInput;
    if (forInput.Length == 1) return firstCharAlterationFunction(forInput);
    return firstCharAlterationFunction(forInput[0].ToString()) + remainingCharsAlterationFunction(forInput.Substring(1));
}
2
  • Can you give it rules? I've found most solutions didn't fit my particular business needs.
    – rory.ap
    Commented May 29, 2018 at 15:50
  • I'm pretty sure the StatusDescription already contains what the OP wants. Even Humanizer may be a bit of overkill - and quite a bit bigger that this code Commented May 29, 2018 at 15:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.