![](/img/trans.png)
[英]Unexpected end of MIME multipart stream. MIME multipart message is not complete. Web API and superagent
[英]API unable to read multipart: Unexpected end of MIME multipart stream. MIME multipart message is not complete
自从这个周末过去以来,我们就开始遇到一个问题。 我们将文件从一台服务器上的应用程序发送到另一台服务器上的 API。
通常此 API 位于 cloudflare 的 CDN/代理之后,但是自 2 月 13 日 01:00-02:00 左右以来,此功能仅在未启用 cloudflare 代理/CDN 的情况下有效(我们实际上已启用它,而是编辑了发件人的主机文件指向我们的真实 IP 地址,而不是 cloudflare 的)。 如果我们删除发件人应用程序并使用邮递员,结果是一样的(使用 cdn 失败,直接工作)。
在他们的支持下,我收到了一张票,但我也想彻底检查一下这可能不是我们做错的事情。
[HttpPost]
[Route("sendvideofile")]
public async Task<CameraResponse> ReceiveVideoFile()
{
var content = await GetMultipartContent(this.Request.Content).EscapeContext();
var stream = await content.ReadAsStreamAsync().EscapeContext();
return CameraResponse.Create(true);
}
这是我们的 GetMultiPartContent 方法:
private static async Task<HttpContent> GetMultipartContent(HttpContent requestContent)
{
var filesReadToProvider = await requestContent.ReadAsMultipartAsync().EscapeContext();
return filesReadToProvider.Contents.FirstOrDefault();
}
抛出的异常是:
Message:An error has occurred.
ExceptionMessage:Unexpected end of MIME multipart stream. MIME multipart message is not complete.
ExceptionType:System.IO.IOException
StackTrace: at System.Net.Http.Formatting.Parsers.MimeMultipartBodyPartParser.<ParseBuffer>d__0.MoveNext()
at System.Net.Http.HttpContentMultipartExtensions.<MultipartReadAsync>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpContentMultipartExtensions......
正如您所看到的,我们的代码实际上非常简单(我已经删除了一些不会导致这种情况的其他专有服务调用),但我很难相信 Cloudflare 可能已经推出了一些导致 dotnet 中的基本方法的东西框架无法读取多部分流。 或者至少如果他们有,我们会听说过它或通过我们的票被告知。
我在 API 上遇到了类似的问题,问题与未关闭的边界有关。
基于这个 SO question what-rules-apply-to-mime-boundary ,边界必须以最终结束 -
我在 POSTman (text/raw) 中有一个初始请求正文,具有以下设置
标题
multipart/form-data;boundary=bee21932-0a3e-45a3-957d-e0b5f3e8f12e
身体
--bee21932-0a3e-45a3-957d-e0b5f3e8f12e
Content-Type: multipart/form-data;boundary="9e73357f-6a62-452b-a1b8-2b48586435e5"
Content-Disposition: form-data
--9e73357f-6a62-452b-a1b8-2b48586435e5
Content-Type: application/x-www-form-urlencoded
Content-Disposition: form-data
DocumentId=3
--9e73357f-6a62-452b-a1b8-2b48586435e5--
--bee21932-0a3e-45a3-957d-e0b5f3e8f12e--
问题是这一直与消息失败
MIME 多部分流的意外结束。 MIME 多部分消息不完整。
因此,在找到提到的 SO 文章后,我更新了正文以增加一个边界:
--bee21932-0a3e-45a3-957d-e0b5f3e8f12e
Content-Type: multipart/form-data;boundary="9e73357f-6a62-452b-a1b8-2b48586435e5"
Content-Disposition: form-data
--9e73357f-6a62-452b-a1b8-2b48586435e5
Content-Type: application/x-www-form-urlencoded
Content-Disposition: form-data
DocumentId=3
--9e73357f-6a62-452b-a1b8-2b48586435e5
--9e73357f-6a62-452b-a1b8-2b48586435e5-- //this was added
--bee21932-0a3e-45a3-957d-e0b5f3e8f12e--
我得到了成功。 (我仍然不确定为什么服务器需要这个,但以防其他人需要这个)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.