c#HTTPListener编码问题

c#HTTPListener编码问题,c#,encoding,httplistener,C#,Encoding,Httplistener,我有一个Java应用程序向C#应用程序发送HTTP请求。C#应用程序使用HTTPListener来监听请求并做出响应。在Java方面,我使用UTF-8对URL进行编码 当我发送\字符时,它会按预期编码为%5C,但在C端它会变成一个/字符。请求对象的编码是Windows-1252,我认为这可能是导致问题的原因。如何将默认编码设置为UTF-8 目前,我正在执行此操作以转换编码: foreach (string key in request.QueryString.Keys)

我有一个Java应用程序向C#应用程序发送HTTP请求。C#应用程序使用HTTPListener来监听请求并做出响应。在Java方面,我使用UTF-8对URL进行编码

当我发送\字符时,它会按预期编码为%5C,但在C端它会变成一个/字符。请求对象的编码是Windows-1252,我认为这可能是导致问题的原因。如何将默认编码设置为UTF-8

目前,我正在执行此操作以转换编码:

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

这会处理我发送的非ASCII字符,但不会解决斜杠问题。在调试器中检查request.QueryString[key]表明/已经存在

将每个查询字符串变量编码为UTF8:

byte[] utf8EncodedQueryBytes = Encoding.UTF8.GetBytes(key);
short: 您必须将
charset=utf-8
添加到响应标题中的内容类型

头应该是这样的。 Content Type=“text/plain;charset=utf-8”

长:

我的http侦听器的响应头

//where is utf-8 ???  i need to put it somewhere here...!!!
Content-Length: 31
Content-Type: text/plain;    
Server: Microsoft-HTTPAPI/2.0
我查看了一个能够显示utf-8字符(如turkishüğşİ)的网页

来自utf-8网站的响应头

content-type: text/html; charset=utf-8    //so we have put it here like this.
content-length: 20270
content-encoding: gzip
...
content-type: text/html; charset=utf-8    //so we have put it here like this.
content-length: 20270
content-encoding: gzip
...