streamReader.ReadToEndAsyn c () изменение содержимого сообщения - PullRequest
2 голосов
/ 04 августа 2020

У меня есть веб-API. Net Core 3.1, которому необходимо взаимодействовать с внешней службой, которая предоставляет составной / связанный ответ от службы WCF с вложениями в контексте, как в блоке ниже.

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-файл становится недействительным \ поврежденным \ пустым.

enter image description here

An additional characteristic of this is the content length going from 1021690 to 1790633.

I have been able to debug it this far and test that the files before they get into the message (string) are correct as I am able to read the multipart content and write the files out to disk when checking that the files are indeed correct using

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);
    }

}

and then iterating through the content.Contents.

I would be grateful if anyone could tell me how \ why this is occurring and how I can stop this from occuring

Further investigation and hypothesis is could this be down to encoding? The WCF service for the attachments is TransferEncoding = "binary"

введите описание изображения здесь

Левая и середина изображения одинаковы, середина, записанная на диск прямо из моего API (не то, что мне нужно), показывает мне, что файл можно читать и записывать.

Справа - прямой вывод службы, который возвращается в SOAP UI и загружается, как ожидалось.

При просмотре ответа в окне просмотра кодировка передачи пуста, поэтому я немного запутался как решить эту проблему.

1 Ответ

1 голос
/ 04 августа 2020

попробуйте это,

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;
    }
}

, а затем в вашем контроллере верните это так

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

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