1

I am using HTTP headers to send a string which contains Unicode characters (such as ñ) to a custom http server.

When I add the string as a header:

webClient.Headers.Add("Custom-Data", "señor");

It is interpreted by the server as:

se�or

Obviously I need to encode the value differently, but I am unsure what encoding to use.

How should I encode this HTTP header to preserve extended/special characters?

3
  • webClient.Headers.Add("Content-Type","text/html; charset=UTF-8"); ? Commented May 2, 2013 at 21:04
  • 1
    @Jordan That just adds another header saying "the body content is html." I need to encode the value of a custom header, not the body/content. Commented May 2, 2013 at 21:11
  • Perhaps you could use something trivial like base64? Commented May 2, 2013 at 21:13

1 Answer 1

2

As @Jordan suggested, representing the string as base64 (with UTF8 encoding) worked well:

On the client side:

webClient.Headers.Add("Custom-Data",
    Convert.ToBase64String(Encoding.UTF8.GetBytes("señor")));

And on the server:

string customData = Encoding.UTF8.GetString(Convert.FromBase64String(customHeader.Value));
Sign up to request clarification or add additional context in comments.

Comments

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.