Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to encode custom HTTP headers in C#

Tags:

c#

.net

http

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

like image 596
antonio Avatar asked May 04 '10 21:05

antonio


People also ask

How are HTTP headers encoded?

HTTP messages are encoded with ISO-8859-1 (which can be nominally considered as an enhanced ASCII version, containing umlauts, diacritic and other characters of West European languages). At the same time, the message body can use another encoding assigned in "Content-Type" header.

How do you pass custom headers?

To pass custom headers from an API Gateway API to a Lambda function, use a body mapping template. The API sends the updated API request to a Lambda function to process the headers. Then, the Lambda function returns one or more header values from the original API request.

Can HTTP headers be custom?

Custom HTTP headers can be used to filter requests or specify a value for the Accept header.


1 Answers

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)

like image 58
liuhongbo Avatar answered Nov 04 '22 03:11

liuhongbo