AllowHttpStatus 仍然抛出 Flurl.Http.FlurlHttpException

AllowHttpStatus still throw Flurl.Http.FlurlHttpException

我正在使用 AllowHttpStatus 来禁用对 HTTP 错误的异常抛出并自行处理异常。不幸的是,我仍然得到一个例外。

Flurl.Http.FlurlHttpException. Call failed with status code 500 (Internal Server Error): POST http://MyUrl Request

密码

var request = new Url("http://MyUrl").AllowHttpStatus();

var content = new FileContent(Conversion.SourceFile.FileInfo.ToString());

var task = request.PostAsync(content, model.CancellationToken);
using (var httpStream = await task.ReceiveStream())
using (var fileStream = new FileStream(DestinationLocation + @"\result." + model.DestinationFileFormat, FileMode.CreateNew))
{
    await httpStream.CopyToAsync(fileStream);
}

//This line is never reached if HTTP Exception is thrown by PostAsync   
if (task.Result.StatusCode != HttpStatusCode.OK)
{
    if (task.Result.StatusCode != HttpStatusCode.InternalServerError)
    {
        Logger.Main.LogCritical($"Exception {task.Result.ReasonPhrase}");
    }

    throw new ApiException(ResponseMessageType.ConversionFailed);
}

为什么 AllowHttpStatus 没有按预期工作?

AllowHttpStatus 接受参数。没有它们,它没有效果。所以在这种情况下你需要传递它 HttpStatusCode.InternalServerError 例如:

var request = new Url("http://MyUrl").AllowHttpStatus(HttpStatusCode.InternalServerError);

或者使用 AllowAnyHttpStatus 代替:

var request = new Url("http://MyUrl").AllowAnyHttpStatus();