Yan*_*ans 2 .net c# encoding restsharp
我正在使用RestSharp版本105.1.0(.NET 4.5.1)对我们自己的API进行REST调用.此API使用以下特别感兴趣的标头发送响应:Content-Type: application/json; Charset=iso-8859-1.如您所见,此响应的字符集设置为iso-8859-1.
我希望我从RestSharp得到的响应使用这种编码来解码响应内容.但是,当我查看RestResponse.Content属性时,某些字符显示为 .据我所知,这意味着使用了错误的编码.当我尝试RawBytes使用正确的编码手动解码时,我得到正确的字符串.
我尝试手动设置iso-8859-1 Encoding属性,RestClient但无济于事.
如何确保使用正确的编码解码RestSharp的响应?
示例代码:
// Setting the Encoding here does not change the result
var client = new RestClient(myApiUri) { Encoding = Encoding.GetEncoding("iso-8859-1") };
var request = new RestRequest(Method.GET);
var restResponse = client.Execute(request);
Console.WriteLine(restResponse.Content)
// Outputs content as string with wrong encoding
// some characters display as ?
Run Code Online (Sandbox Code Playgroud)
提前致谢!
小智 7
我也有这个问题,要解决必须得到它带来的IRestResponse对象的字节数组并将其转换为我想要的编码
var request = new RestRequest(Method.GET);
var restResponse = client.Execute(request);
Encoding encoding = Encoding.GetEncoding("ISO-8859-1");
var result = encoding.GetString(response.RawBytes);
Console.WriteLine(result);
Run Code Online (Sandbox Code Playgroud)