开发者

StatusDescription Encoding in HttpResponse

开发者 https://www.devze.com 2023-03-03 09:26 出处:网络
I am using C# and write to HttpRespon开发者_如何学Pythonse StatusDescription the error message in case an exception occurs. Internal i use another service that returns error descriptions in russian un

I am using C# and write to HttpRespon开发者_如何学Pythonse 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?


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:

response.StatusDescription =
  exception.Message.Substring(0, Math.Min(exception.Message.Length, 512));

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消