Azure 逻辑应用/Azure 功能:当我读取 HTTPRequest 主体时截断主体

问题描述 投票:0回答:1

我有一个azure逻辑应用程序,我使用一个azure函数来计算我之前从另一个软件(API REST)下载的PDF文件的SHA256。

我的问题是,下载请求的结果只是正文中的文件,当我将其传递给我的函数时,其中的字节比原始文件少。

最后,SHA256 无效,我的逻辑应用程序的下一步(使用 REST API 将文件推送到另一个软件)未运行,因为该软件计算文件的 SHA256 并且与我的 azure 不一样功能。

在此过程中PDF本身没有损坏,我们可以打开它。

这是我的函数的日志:

azureFunctionLog

我的问题是,为什么阅读后长度变短了?

这是我的代码:

using System;
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using System.Security.Cryptography;
using System.Text;

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");
    log.LogInformation("Length Req.body: " + req.Body.Length);
    log.LogInformation("Content Req.body: " + req.Body);

    var reader = new StreamReader(req.Body);
    reader.BaseStream.Seek(0, SeekOrigin.Begin); 
    var requestBody = reader.ReadToEnd();

    
    log.LogInformation("Length : " + requestBody.Length);

    using var hash = SHA256.Create();
    var byteArray = hash.ComputeHash(Encoding.UTF8.GetBytes(requestBody));
    string hex = Convert.ToHexString(byteArray);
    

    return new OkObjectResult(hex);
}

谢谢!

我尝试用此代码更改阅读器功能:

string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

同样的问题。

c# azure-functions azure-logic-apps sha256 streamreader
1个回答
0
投票

谢谢你的回答。

同时我找到了解决方案。

Indedi 做了一个糟糕的比较,转换后的字节数比我一开始的字节数要少。

所以有正确运行的代码:

using var hash = SHA256.Create();
var byteArray = hash.ComputeHash(req.Body);
string hex = Convert.ToHexString(byteArray).ToLower();


return new OkObjectResult(hex);
© www.soinside.com 2019 - 2024. All rights reserved.