首页 > 解决方案 > 从 Flurl 重放 HttpCall

问题描述

我正在编写一个错误处理程序来处理令牌刷新。当我收到expired_token错误时,我正在刷新令牌,我想重播请求,但我不确定如何

public async Task HandleErrorAsync(HttpCall call)
{
      var exception = call.Exception;
      if (exception is FlurlHttpException)
      {
         FlurlHttpException ex = (exception as FlurlHttpException);
         var errorResponse = await ex.GetResponseJsonAsync<ErrorResponse>();

         if(errorResponse.Errors.Any(x => x.Id == EXPIRED_TOKEN))
         {
             await this.RefreshOAuthToken();
             //How can I Replay the request
             //call.Response = call.Request.Replay(); 
             call.ExceptionHandled = true;
         }
     }
}

刷新令牌后,我可以访问刚刚抛出过期令牌错误的 HttpCall 对象。我想重播请求并替换响应,但我不知道该怎么做。

如何在 Flurl 中重放来自 HttpCall 的请求?

标签: c#.neterror-handlingflurl

解决方案


我发现一个重载一般发送我的请求,我做了一个扩展方法

public static async Task<HttpCall> Replay(this HttpCall call)
{
    call.Response = await call.FlurlRequest.SendAsync(call.Request.Method, call.Request.Content);
    return call;
}

推荐阅读