c# HTTPListener encoding issue
- by Rob Griffin
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.