c# – 使用RestSharp时怎么习惯地处理HTTP错误代码?
我正在使用RestSharp构建一个HTTP API客户端,我注意到,当服务器返回HTTP错误代码(401未经授权,404未找到,内部服务器错误等)时,RestClient.Execute()不会抛出一个异常 – 而是使用一个空的.Data属性获取一个有效的RestResponse.我不想手动检查我的API客户端中的每个可能的HTTP错误代码 – RestSharp是否提供了一种将这些错误传递到我的客户端应用程序的更好方法?
一点点细节. RestSharp公开一个Response.ErrorException属性 – 如果RestClient.Execute< T&()调用引发任何异常,它将通过ErrorException属性公开,而不是抛出.他们的文档包括以下示例: // TwilioApi.cs public class TwilioApi { const string BaseUrl = "https://api.twilio.com/2008-08-01"; public T Execute<T>(RestRequest request) where T : new() { var client = new RestClient(); client.BaseUrl = BaseUrl; client.Authenticator = new HttpBasicAuthenticator(_accountSid,_secretKey); request.AddParameter("AccountSid",_accountSid,ParameterType.UrlSegment); // used on every request var response = client.Execute<T>(request); if (response.ErrorException != null) { const string message = "Error retrieving response. Check inner details for more info."; var twilioException = new ApplicationException(message,response.ErrorException); throw twilioException; } return response.Data; } } 我在代码中采用了这种模式,但是我的API服务器返回了401 Unauthorized,但是ErrorException属性仍然为空.我可以在RestResponse.StatusCode和RestResponse.StatusDescription属性中看到未经授权的状态代码和错误消息 – 但是我为什么未经授权的响应不会导致ErrorException字段被填充,我感到困惑. 解决方法
尝试为RestSharp WebAPI客户端创建一个通用的错误处理程序时遇到同样的问题.给出这些扩展方法:
public static class RestSharpExtensionMethods { public static bool IsSuccessful(this IRestResponse response) { return response.StatusCode.IsSuccessStatusCode() && response.ResponseStatus == ResponseStatus.Completed; } public static bool IsSuccessStatusCode(this HttpStatusCode responseCode) { int numericResponse = (int)responseCode; return numericResponse >= 200 && numericResponse <= 399; } } 我提出要求反应是反序列化的: public async Task<ResponseModel<TResponse>> PerformRequestAsync<TResponse>(IRestRequest request) { var response = await _client.ExecuteTaskAsync<ResponseModel<TResponse>>(request); ResponseModel<TResponse> responseData; if (response.IsSuccessful()) { responseData = response.Data; } else { string resultMessage = HandleErrorResponse<TResponse>(request,response); responseData = new ResponseModel<TResponse> { Success = false,ResultMessage = resultMessage }; } return responseData; } 但是,在测试期间,我发现当我没有为这种情况配置错误处理时,当请求未映射的URL时,我的Web服务返回HTML格式的404页面.这导致response.ErrorException属性包含以下字符串:
显然,RestSharp尝试将响应解析为XML,即使内容类型是text / html.也许我会给RestSharp提出一个问题. 当然在生产中,你不应该在打电话给自己的服务时得到404,但是我希望这个客户端是彻底的,可重用的. 所以我可以想到两个解决方案: >检查状态代码并显示说明 前者是很容易完成的.在HandleErrorResponse()中,我根据状态代码的数值构建结果消息(用户可呈现)和错误字符串(loggable): public string HandleErrorResponse(IRestRequest request,IRestResponse response) { string statusString = string.Format("{0} {1} - {2}",(int)response.StatusCode,response.StatusCode,response.StatusDescription); string errorString = "Response status: " + statusString; string resultMessage = ""; if (!response.StatusCode.IsScuccessStatusCode()) { if (string.IsNullOrWhiteSpace(resultMessage)) { resultMessage = "An error occurred while processing the request: " + response.StatusDescription; } } if (response.ErrorException != null) { if (string.IsNullOrWhiteSpace(resultMessage)) { resultMessage = "An exception occurred while processing the request: " + response.ErrorException.Message; } errorString += ",Exception: " + response.ErrorException; } // (other error handling here) _logger.ErrorFormat("Error response: {0}",errorString); return resultMessage; } 现在,由于我的API响应总是包装在一个ResponseModel< T>我可以使用set up an exception filter and a NotFound route在ResultMessage属性中返回带有错误或异常消息的可解析响应模型: public class HandleErrorAttribute : ExceptionFilterAttribute { public override void OnException(HttpActionExecutedContext context) { // (log context.Exception here) context.Response = context.Request.CreateResponse(HttpStatusCode.InternalServerError,new ResponseModel<object> { Success = false,ResultMessage = "An exception occurred while processing the request: " + context.Exception.Message }); } } 和: public class ErrorController : ApiController { public HttpResponseMessage Handle404() { const string notFoundString = "The requested resource could not be found"; var responseMessage = Request.CreateResponse(HttpStatusCode.NotFound,ResultMessage = notFoundString }); responseMessage.ReasonPhrase = notFoundString; return responseMessage; } } 这样,我的服务的响应总是可以由RestSharp解析,我可以使用通用日志记录方法: public string HandleErrorResponse<TResponseModel>(IRestRequest request,IRestResponse<<ResponseModel<TResponseModel>> response) 并记录实际响应//(其他错误处理),如果可用: if (response.Data != null && !string.IsNullOrWhiteSpace(response.Data.ResultMessage)) { resultMessage = response.Data.ResultMessage; errorString += string.Format(",Service response: "{0}"",response.Data.ResultMessage); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |