【发布时间】:2019-03-16 02:18:03
【问题描述】:
我正在使用 RestClient.Execute() 方法运行 Rest 调用,JSON 数据包含带有特殊字符的重音。
通过 SoapUI 进行相同的调用,我没有得到带有损坏字符的返回。
我的代码:
public ProviderRestResponse<Response> PostService(ProviderRestRequest request)
{
var response = new ProviderRestResponse<Response>();
try
{
var client = new RestClient(request.UrlService);
var restRequest = new RestRequest(Method.POST);
restRequest.Parameters.Clear();
var dadosDessao = new
{
request.DadosSessao
};
restRequest.AddJsonBody(request.Parameters);
restRequest.AddJsonBody(dadosDessao);
var restResponse = client.Execute(restRequest);
if (restResponse.ResponseStatus == ResponseStatus.Error)
{
throw new Exception(restResponse.ErrorMessage, restResponse.ErrorException);
}
var obj = ((dynamic)JsonConvert.DeserializeObject(restResponse.Content)).Resultado;
response.Data = JsonConvert.DeserializeObject<Response>(JsonConvert.SerializeObject(obj));
response.Content = restResponse.Content;
response.ResponseType = ResponseType.Success;
}
catch (Exception ex)
{
response.Message = ex.Message;
response.ResponseType = ResponseType.Error;
}
return response;
}
谢谢!
【问题讨论】:
-
在执行您的请求时尝试启用
utf-8数据编码 -
回复:stackoverflow.com/questions/37720731/…,我根据你的提名搜索了。由于该帖子的作者制作了链接,我认为这也是一种更清洁的解决方案。感谢您的建议!
-
对你的情况有用吗?
-
是的。但我找到了链接“猪”的代码(我们在巴西为错误代码提供的名称)。
标签: c# asp.net .net asp.net-mvc web-services