16

Is there a class similar to HttpUtility to encode the content of a custom header? Ideally I would like to keep the content readable.

0

5 Answers 5

10

You can use the HttpEncoder.HeaderNameValueEncode Method in the .NET Framework 4.0 and above.

For previous versions of the .NET Framework, you can roll your own encoder, using the logic noted on the HttpEncoder.HeaderNameValueEncode reference page:

  • All characters whose Unicode value is less than ASCII character 32, except ASCII character 9, are URL-encoded into a format of %NN where the N characters represent hexadecimal values.

  • ASCII character 9 (the horizontal tab character) is not URL-encoded.

  • ASCII character 127 is encoded as %7F.

  • All other characters are not encoded.

Update:

As OliverBock point out the HttpEncoder.HeaderNameValueEncode Method is protected and internal. I went to open source Mono project and found the mono's implementation

void HeaderNameValueEncode (string headerName, string headerValue, out string encodedHeaderName, out string encodedHeaderValue)
{
        if (String.IsNullOrEmpty (headerName))
                encodedHeaderName = headerName;
        else
                encodedHeaderName = EncodeHeaderString (headerName);

        if (String.IsNullOrEmpty (headerValue))
                encodedHeaderValue = headerValue;
        else
                encodedHeaderValue = EncodeHeaderString (headerValue);
}

static void StringBuilderAppend (string s, ref StringBuilder sb)
{
        if (sb == null)
                sb = new StringBuilder (s);
        else
                sb.Append (s);
}

static string EncodeHeaderString (string input)
{
        StringBuilder sb = null;

        for (int i = 0; i < input.Length; i++) {
                char ch = input [i];

                if ((ch < 32 && ch != 9) || ch == 127)
                        StringBuilderAppend (String.Format ("%{0:x2}", (int)ch), ref sb);
        }

        if (sb != null)
                return sb.ToString ();

        return input;
}

Just FYI

[here ] (https://github.com/mono/mono/blob/master/mcs/class/System.Web/System.Web.Util/HttpEncoder.cs)

Sign up to request clarification or add additional context in comments.

3 Comments

Sadly neither this version nor the .NET version (seen via Reflector) bother to properly encode '%' characters already in the data.
Is it me, or does EncodeHeaderString return a string containing only characters that are to be encoded, and doesn't include anything part of input that isn't less than 32, but not 9, and 127 (ASCII).
@GCymbala - Yes I agree. The inner loop appears to be missing an else StringBuilderAppend(ch, ref sb);. I do not see how the current "encoded" result would have any possible use.
7

For me helped Uri.EscapeDataString(headervalue)

Comments

0

This does the same job as HeaderNameValueEncode(), but will also encode % characters so the header can be reliably decoded later.

static string EncodeHeaderValue(string value)
{
    return Regex.Replace(value, @"[\u0000-\u0008\u000a-\u001f%\u007f]", (m) => "%"+((int)m.Value[0]).ToString("x2"));
}

static string DecodeHeaderValue(string encoded)
{
    return Regex.Replace(encoded, @"%([0-9a-f]{2})", (m) => new String((char)Convert.ToInt32(m.Groups[1].Value, 16), 1), RegexOptions.IgnoreCase);
}

1 Comment

This doens't seem right. Shouldn't it be encoding things that AREN'T in that range? If I reverse the regex and encode this £a£a£a I end up with %a3%61%a3%61%a3%61 which isn't ideal because I'd prefer the a not be encoded at all
-2

Mayeb This one ? UrlEncode Function

1 Comment

I am looking for a method that does not perform the encoding required for the URL but just considers the specific limitatons of the HTTP headers
-4

sorry its off the top of my head but for your request object there should be a headers object you can add to.

i.e. request.headers.add("blah");

Thats not spot on but it should point you in the right direction.

1 Comment

The question is about encoding the header value, not about adding a header.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.