使用Flurl.Http,有没有办法确定发送的字节数?



我想知道在使用Post或PostAsync时实际传输了多少字节。 我正在使用类似于以下内容的代码。我可以查看 filePath 的字节,但在我的实际代码中,我在读取和发送之间对文件流进行了一些操作。 如果你拉出MyFilteredContent线,你会怎么做?

async Task<bool> SendFile(string filePath)
{
using (HttpContent fileContent = new FileContent(filePath))
using (MyFilteredContent filteredContent = new MyFilteredContent(fileContent))
{
var t = await MyAppSettings.TargetUrl
.AllowAnyHttpStatus()
.PostAsync(filteredContent);
if (t.IsSuccessStatusCode)
{
return true;
}
throw new Exception("blah blah");
}
}

这是我在注释中描述的代码示例 - 使用委派处理程序,覆盖 SendAsync 以获取正在发送的请求的字节数,然后配置 FlurlHttp 设置以使用处理程序:

public class HttpFactory : DefaultHttpClientFactory
{
public override HttpMessageHandler CreateMessageHandler()
{
return new CustomMessageHandler();
}
}
public class CustomMessageHandler : DelegatingHandler
{
protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var content = await request.Content.ReadAsByteArrayAsync();

return await base.SendAsync(request, cancellationToken);
}
}
FlurlHttp.Configure(settings =>
{
settings.HttpClientFactory = new HttpFactory();
});

最新更新