Microsoft.AspNetCore.WebUtilities超出了行长度限制100

时间:2017-06-20 02:09:32

标签: c# asp.net asp.net-core-1.0

我一直在尝试自动化测试,将文本文件上传到使用flowjshandler的web api,所有这些都在c#.net核心和linux docker容器中运行。从visual studio运行时,测试程序无异常地上传文件。但是,如果我在容器中运行测试程序和web api,我会在asp .net核心web api框架中获得此异常:

  

超出了行长度限制。,Microsoft.AspNetCore.WebUtilities,
  在   Microsoft.AspNetCore.WebUtilities.BufferedReadStream.d__39.MoveNext()

我查看Microsoft.AspNetCore.WebUtilities并找到错误的来源,但不确定如何避免100个字符限制。我试图改变上传文件。

这是包含内容的多部分消息

Content-Type: multipart/form-data; boundary=----WebKitFormBoundarym45GFZc25WVhjtVB
------WebKitFormBoundarym45GFZc25WVhjtVB
Content-Disposition: form-data; name="flowChunkNumber"

1
------WebKitFormBoundarym45GFZc25WVhjtVB
Content-Disposition: form-data; name="flowChunkSize"

1048576
------WebKitFormBoundarym45GFZc25WVhjtVB
Content-Disposition: form-data; name="flowCurrentChunkSize"

440
------WebKitFormBoundarym45GFZc25WVhjtVB
Content-Disposition: form-data; name="flowTotalSize"

440
------WebKitFormBoundarym45GFZc25WVhjtVB
Content-Disposition: form-data; name="flowIdentifier"

440-Boundarystxt
------WebKitFormBoundarym45GFZc25WVhjtVB
Content-Disposition: form-data; name="flowFilename"

Boundarys.txt
------WebKitFormBoundarym45GFZc25WVhjtVB
Content-Disposition: form-data; name="flowRelativePath"

Boundarys.txt
------WebKitFormBoundarym45GFZc25WVhjtVB
Content-Disposition: form-data; name="flowTotalChunks"

1
------WebKitFormBoundarym45GFZc25WVhjtVB
Content-Disposition: form-data; name="file"; filename="Boundarys.txt"

2 个答案:

答案 0 :(得分:1)

我在linux容器上运行时使用Environment.Newline无法识别行尾。在Content-Disposition:form-data中将换行符更改为\r\n

var nl = "\r\n";
sb.AppendFormat($"{nl}{BOUNDARY}{nl}Content-Disposition: form-data; name=\"flowChunkNumber\"{nl}{nl}{flowFileUpload.flowChunkNumber}{nl}{BOUNDARY}{nl}Content-Disposition: form-data; 

答案 1 :(得分:0)

重要的是要知道为什么没有“ \ r \ n”失败了。请看以下规格:https://tools.ietf.org/html/rfc2046#section-4.1.1

The canonical form of any MIME "text" subtype MUST always represent a
line break as a CRLF sequence.  Similarly, any occurrence of CRLF in
MIME "text" MUST represent a line break.  Use of CR and LF outside of
line break sequences is also forbidden.

因此,这取决于您如何构建此字符串。如果您使用StringBuilder并通过AppendLine()逐行构建请求,那么它将在Windows上有效,但在Linux上无效。 AppendLine使用Environment.NewLine,它是Windows的CRLF和Linux的LF。在幕后,BufferedReadStream会困扰于根据规范CRLF划定的新行。因此,在这种情况下,您需要明确使用\ r \ n。