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

C# 使用Flurl.Http,有没有办法确定发送的字节数?,c#,flurl,C#,Flurl,我想知道使用Post或PostAsync时实际传输了多少字节。我使用的代码与下面类似。我可以查看文件路径的字节,但在我的实际代码中,我在读取和发送之间对文件流进行一些操作。如果拉出MyFilteredContent行,您将如何操作 async Task<bool> SendFile(string filePath) { using (HttpContent fileContent = new FileContent(filePath)) using (MyFilter

我想知道使用Post或PostAsync时实际传输了多少字节。我使用的代码与下面类似。我可以查看文件路径的字节,但在我的实际代码中,我在读取和发送之间对文件流进行一些操作。如果拉出
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");
    }
}
异步任务发送文件(字符串文件路径)
{
使用(HttpContent fileContent=newfilecontent(filePath))
使用(MyFilteredContent filteredContent=新MyFilteredContent(fileContent))
{
var t=await MyAppSettings.TargetUrl
.AllowAnyHttpStatus()
.PostAsync(过滤内容);
如果(t.IsSuccessStatusCode)
{
返回true;
}
抛出新异常(“诸如此类”);
}
}

下面是我在评论中描述的代码示例—使用DelegatingHandler,重写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();
 });
公共类HttpFactory:DefaultHttpClientFactory
{
公共重写HttpMessageHandler CreateMessageHandler()
{
返回新的CustomMessageHandler();
}
}
公共类CustomMessageHandler:DelegatingHandler
{
受保护的异步覆盖任务SendAsync(HttpRequestMessage请求,CancellationToken CancellationToken)
{
var content=wait request.content.ReadAsByteArrayAsync();
返回wait base.sendaync(请求、取消令牌);
}
}
FlurlHttp.Configure(设置=>
{
settings.HttpClientFactory=新的HttpFactory();
});

正在使用DelegatingHandler,重写SendAsync以获取发送的请求的字节,然后配置FlurlHttp设置以使用处理程序选项..?听起来不错。如何做?我们如何将授权管理者联系在一起?我已经重写CreateMessageHandler以提供代理支持。我得到了它--我必须添加一个构造函数来链接.Yep-通过构造函数传入其他处理程序并设置InnerHandlerOK--新问题。如果我从CustomMessageHandler中的SendAsync重写内部获取content.LongLength,那么如何将其传递给最初调用SendAsync的方法?有没有一种方法可以从调用者那里访问它?您可以在响应消息的某个地方插入它:var httpResponseMessage=wait base.SendAsync(请求,取消令牌);然后从SendAsync返回httpResponseMessage