HttpContent.ReadAsByteArrayAsync()失败,在DelegatingHandler内部没有错误 [英] HttpContent.ReadAsByteArrayAsync() fails without error inside DelegatingHandler

查看:111
本文介绍了HttpContent.ReadAsByteArrayAsync()失败,在DelegatingHandler内部没有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为API实现HMAC安全性.一切正常,直到我尝试发布文件为止.

I'm trying to implement HMAC security for an API. Everything works fine until I try to post a file.

可以在此处找到HMAC解决方案- https://github.com/gavinharriss/WebAPI.HMAC -与原始版本不同,它既允许GET请求也允许POST请求.

The HMAC solution can be found here - https://github.com/gavinharriss/WebAPI.HMAC - it's a fork from the original to allow GET requests as well as POST requests.

附加文件的代码:

var requestContent = new MultipartFormDataContent();
var fileContent = new ByteArrayContent(file);
requestContent.Add(fileContent, "file", filename);

如果我立即致电HttpContent.ReadAsByteArrayAsync()没问题,则字节数组可用.

if I immediately call HttpContent.ReadAsByteArrayAsync() there is no issue, the byte array is available.

但是,HMAC HttpClient( HMACHttpClient )实现了DelegatingHandler( HMACDelegatingHandler ),以便将HMAC标头附加到请求.

However, the HMAC HttpClient (HMACHttpClient) implements a DelegatingHandler (HMACDelegatingHandler) in order to attach the HMAC header to requests.

HMACDelegatingHandler 中请求以HttpRequestMessage的形式传递,在帮助程序中使用HttpRequestMessage.Content属性来构建HMAC签名.

In the HMACDelegatingHandler the request is passed along as a HttpRequestMessage from which the HttpRequestMessage.Content property is used in a helper to build the HMAC signature.

构建签名时,从帮助程序类:

private static async Task<byte[]> ComputeHash(HttpContent httpContent)
{
    using (var md5 = MD5.Create())
    {
        byte[] hash = null;
        if (httpContent != null)
        {
            var content = await httpContent.ReadAsByteArrayAsync(); // <-- Fails here
            if (content.Length != 0)
            {
                hash = md5.ComputeHash(content);
            }
        }
        return hash;
    }
}

单步执行代码时,按下var content = await httpContent.ReadAsByteArrayAsync()行,然后什么也没有,没有错误.这些请求似乎很po琐,但一切仍在运行,并且HttpClient请求从未发送过.

When stepping through the code the var content = await httpContent.ReadAsByteArrayAsync() line is hit, then nothing, no error. The requests just seems to go poof but everything is still running and the HttpClient request never gets sent.

有什么想法吗?

推荐答案

在对各种大小的文件进行了测试之后,我发现当文件大小达到50,000字节左右时出现了问题.

Having tested this with various sizes of file, I found the issue arose when files got around the 50,000 byte mark.

这篇文章提供了一个解决方案: HttpContent.ReadAsStringAsync导致请求挂起(或其他奇怪行为).

This post provided a solution: HttpContent.ReadAsStringAsync causes request to hang (or other strange behaviours).

如果您替换HMACHelper中的错误行(第66行):

If you replace erroring line in HMACHelper (line 66):

var content = await httpContent.ReadAsByteArrayAsync();

与此:

var ms = new MemoryStream();
await httpContent.CopyToAsync(ms);
ms.Seek(0, SeekOrigin.Begin);

var content = ms.ToArray();

它应该停止挂起.

这篇关于HttpContent.ReadAsByteArrayAsync()失败,在DelegatingHandler内部没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆