streamReader.ReadToEndAsync() 更改消息内容

streamReader.ReadToEndAsync() changing message content

我有一个 .Net Core 3.1 Web API 需要与外部服务交互,该外部服务提供来自 WCF 服务的 multipart/related 响应,上下文中的附件如下块。

HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 1790633
Content-Type: multipart/related; type="text/xml"; start="<8fb5b3efa6474add9032cb80870dc065>"; boundary="------=_Part_20200731120159.494997"
Server: Microsoft-IIS/10.0
Set-Cookie: ASP.NET_SessionId=jga2opbhoirgnhjhmmsuxi1k; path=/; HttpOnly; SameSite=Lax
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Set-Cookie: ARRAffinity=f7b4f7f5d8cbd8c8bc5d9a4bf49e8b5938412c4294f6e2c2b5c2d5cfc4b4d437;Path=/;HttpOnly;Domain=lparouterpoc.azurewebsites.net
Date: Fri, 31 Jul 2020 13:33:04 GMT


--------=_Part_20200731120159.494997
Content-Type: text/xml; charset=UTF-8
Content-Transfer-Encoding: binary
Content-Id: <8fb5b3efa6474add9032cb80870dc065>

<?xml version="1.0" encoding="iso-8859-1"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
...
</SOAP-ENV:Envelope>
--------=_Part_20200731120159.494997
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-Id: 7b1fff220df5@xxxxx

%PDF-1.5
%����
...
Additional characters added in here
...
%%EOF

--------=_Part_20200731120159.494997
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-Id: 844eb478dc05@xxxxx

%PDF-1.5
...
Additional characters added here
...
%%EOF
--------=_Part_20200731120159.494997--

使用 SOAP UI,我看到有附件,但每个附件都是空白 PDF。

.Net Core API中的代码是

using (var response = await Client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead))
{
    var stream = await response.Content.ReadAsStreamAsync();
    using var streamReader = new StreamReader(stream, Encoding.UTF8);
    var message = await streamReader.ReadToEndAsync(); // This is where output is changed to add additional characters
    var headers = response.Headers;
    return new customResponse
    {
        ContentResult = new ContentResult
        {
            Content = message,
            ContentType = response.Content.Headers.ContentType?.ToString(),
            StatusCode = (int)response.StatusCode
        },
        ResponseHeaders = headers
    };
}

下图显示了一个示例,其中出现了额外的字符,导致 PDF 无效\损坏\空白。

它的另一个特点是内容长度从 1021690 到 1790633。

到目前为止,我已经能够调试它并在文件进入消息(字符串)之前测试文件是否正确,因为我能够读取多部分内容并将文件写出到磁盘,同时检查使用

文件确实正确
var content = await response.Content.ReadAsMultipartAsync();
for (var i = 0; i < content.Contents.Count; i++)
{
    var item = content.Contents[i];
    if (item.Headers.ContentType.ToString() == "application/octet-stream")
    {
        var bytes = await item.ReadAsByteArrayAsync();
        File.WriteAllBytes(@$"c:\temp\{i}.pdf", bytes);
    }

}

然后遍历 content.Contents.

如果有人能告诉我这是怎么发生的、为什么会发生以及我该如何阻止它发生,我将不胜感激

进一步的调查和假设是这可能归结为编码吗?附件的 WCF 服务是 TransferEncoding = "binary"

图像的左侧和中间是一样的,中间直接从我的API(不是我需要的)写入磁盘显示我可以读取和写入文件。

右边是服务的直接输出,返回到 SOAP UI 并按预期下载。

查看手表中的响应时 window 传输编码为空,所以我对如何解决这个问题有点困惑。

试试这个,

public async Task<HttpResponseMessage> Post(string requestBody, string action)
{
    using (var request = new HttpRequestMessage(HttpMethod.Post, ""))
    {
        request.Headers.Add("SOAPAction", action);
        request.Content = new StringContent(requestBody, Encoding.UTF8, "text/xml");
        var response = await Client.SendAsync(request);
        return response;
    }
}

然后在你的控制器中return它是这样的

var result = await _service.Post(requestBody, action);

var stream = await result.Content.ReadAsStreamAsync();
await stream.CopyToAsync(HttpContext.Response.Body);