c# HTTPListener encoding issue

Posted by Rob Griffin on Stack Overflow See other posts from Stack Overflow or by Rob Griffin
Published on 2010-04-23T06:01:15Z Indexed on 2010/04/23 6:03 UTC
Read the original article Hit count: 374

Filed under:
|
|

I have a Java application sending HTTP requests to a C# application. The C# app uses HTTPListener to listen for requests and respond. On the Java side I'm encoding the URL using UTF-8.

When I send a \ character it gets encoded as %5C as expected but on the C# side it becomes a / character. The encoding for the request object is Windows-1252 which I think may be causing the problem. How do I set the default encoding to UTF-8?

Currently I'm doing this to convert the encoding:

        foreach (string key in request.QueryString.Keys)
        {
            if (key != null)
            {
                byte[] sourceBytes =request.ContentEncoding.GetBytes(request.QueryString[key]);
                string value = Encoding.UTF8.GetString(sourceBytes));
             }
        }

This handles the non ASCII characters I'm also sending but doesn't fix the slash problem. Examining request.QueryString[key] in the debugger shows that the / is already there.

© Stack Overflow or respective owner

Related posts about c#

Related posts about httplistener