C# 为什么响应不';是否不包含“内容编码”标题?

C# 为什么响应不';是否不包含“内容编码”标题?,c#,.net,.net-core,C#,.net,.net Core,为什么响应不包含内容编码标题 using System.Net; using System.Text; namespace MyServer { class Program { static void Main(string[] args) { var listener = new HttpListener(); listener.Prefixes.Add("http://localhost:

为什么响应不包含
内容编码
标题

using System.Net;
using System.Text;

namespace MyServer
{
    class Program
    {
        static void Main(string[] args)
        {
           var listener = new HttpListener();
           listener.Prefixes.Add("http://localhost:8001/");
           listener.Start();
           var context = listener.GetContext();
           var response = context.Response;
           response.ContentEncoding = Encoding.UTF8;
           response.ContentType = "application/json";
           var json = "{'message':'Привет, мир!'}";
           var jsonBytes = response.ContentEncoding.GetBytes(json);
           response.ContentLength64 = jsonBytes.LongLength;
           response.StatusCode = (int)HttpStatusCode.OK;
           response.OutputStream.Write(jsonBytes,0,jsonBytes.Length);
           response.OutputStream.Close();
        }
    }
}

在这种情况下,UTF-8不是有效的内容编码

另见:


在这种情况下,UTF-8不是有效的内容编码

另见:


另外,您可以这样指定编码

response.ContentType = "application/json; charset=utf-8";
更多信息请访问


但正如上面所说:json响应的字符集是冗余的

而且,您可以这样指定编码

response.ContentType = "application/json; charset=utf-8";
更多信息请访问


但正如上面所说:json响应的字符集是冗余的

这是否回答了您的问题@PavelAnikhouski,no.
HttpListenerResponse.ContentEncoding
属性具有
编码类型
。这是否回答了您的问题@PavelAnikhouski,no.
HttpListenerResponse.ContentEncoding
属性具有
编码类型
.Hm。。。但是
HttpListenerResponse.ContentEncoding
属性具有
Encoding
类型。它不能是gzip、compress等。在.NET中,这个属性似乎应该指向编码。。。但是
HttpListenerResponse.ContentEncoding
属性具有
Encoding
类型。不能是gzip、compress等。在.NET中,这个属性似乎应该指向编码。