HttpResponse 中的 StatusDescription 编码
我正在使用 C# 并将错误消息写入 HttpResponse StatusDescription,以防发生异常。不幸的是,内部我使用另一个服务返回俄语错误描述。我必须在 HttpResponse StatusDecrition 中返回此俄语错误消息。我使用UTF-8编码并这样写:
var message = exception.Message.Substring(0, Math.Min(exception.Message.Length, 512));
var encodedBytes = Encoding.UTF8.GetBytes(message);
response.StatusDescription = Encoding.UTF8.GetString(encodedBytes);
但这没有帮助。在 Fiddler 中,编码似乎有问题:
HTTP/1.1 500 无法更新预订。错误消息:������������ ������ �� �������������
������������ ������ �� ������������� 我应该怎样做才能启用俄语符号?
I am using C# and write to HttpResponse StatusDescription the error message in case an exception occurs. Internal i use another service that returns error descriptions in russian unfortunately. And this russian error messages i have to return in HttpResponse StatusDescrition. I use UTF-8 encoding and write this way:
var message = exception.Message.Substring(0, Math.Min(exception.Message.Length, 512));
var encodedBytes = Encoding.UTF8.GetBytes(message);
response.StatusDescription = Encoding.UTF8.GetString(encodedBytes);
But this doesn't help. In Fiddler it seems something wrong with enconding:
HTTP/1.1 500 Could not update reservation. Error message: ����������� ������ �� �������������
What should i do to enable russian symbols?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,您没有使用 UTF-8 编码。在使用字符串之前,您将使用它对字符串进行编码和解码,因此该步骤毫无意义。您的代码的作用与:
无论如何,您不能对 HTTP 标头使用 UTF-8 编码,它们只是纯文本。将错误消息放在响应正文中,因为您可以为其指定编码。
No, you are not using UTF-8 encoding. You are using it to encode and decode the string before you use it, so that step is pointless. Your code does the same as:
Anyway, you can't use UTF-8 encoding for the HTTP headers, they are just plain text. Put the error message in the response body instead, as you can specify an encoding for that.
实际上,有一种方法可以做到这一点:将其编码为 URI 字符串。
您必须在 ECMAScript 中对客户端的响应进行解码:
decodeURIComponent(r.statusText)
Actually, there is a way to do this: encoding it as a URI string.
You will have to decode the response on the client side in ECMAScript:
decodeURIComponent(r.statusText)