的WebAPI无法解析的multipart / form-data发布 [英] WebAPI cannot parse multipart/form-data post

查看:5886
本文介绍了的WebAPI无法解析的multipart / form-data发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图接受来自客户端(iOS应用)后,我的code不停地进行故障上读取数据流。表示该消息是不完整的。我一直试图让这个工作了几个小时,好像事情是错误的,我的消息格式或东西。所有我想要做的是阅读一个字符串,但我与谁在做iOS​​的部分只知道如何发送的multipart / form-data的不是内容类型的JSON工作的开发者。

下面是确切的错误:


  MIME多流

意外结束。 MIME多的信息是不完整的。


它未能在这里:等待Request.Content.ReadAsMultipartAsync(供应商);

标题:

  POST HTTP://本地主机:8603 / API /登录HTTP / 1.1
主机:本地主机:8603
接受编码:gzip,紧缩
内容类型:多重/ form-data的;边界= ------------ NX-oauth216807
内容长度:364
接受语言:EN-US
接受:* / *
连接:保持活动

正文:

  -------------- NX-oauth216807
内容处置:表格数据; NAME =令牌CAAH5su8bZC1IBAC3Qk4aztKzisZCd2Muc3no4BqVUycnZAFSKuleRU7V9uZCbc8DZCedYQTIFKwJbVZCANJCs4ZCZA654PgA22Nei9KiIMLsGbZBaNQugouuLNafNqIOTs9wDvD61ZA6WSTd73AVtFp9tQ1PmFGz601apUGHSimYZCjLfGBo40EBQ5z6eSMNiFeSylym1pK4PCvI17fXCmOcRix4cs96EBl8ZA1opGKVuWizOsS0WZCMiVGvT
-------------- NX-oauth216807--

下面是的WebAPI code:

 公共异步任务< Htt的presponseMessage> PostFormData()
    {
        //检查请求包含的multipart / form-data的。
        如果(!Request.Content.IsMimeMultipartContent())
        {
            抛出新的Htt presponseException(的HTTPStatus code.UnsupportedMediaType);
        }
        尝试
        {
        串根= HttpContext.Current.Server.MapPath(〜/ App_Data文件);
        VAR提供商=新MultipartFormDataStreamProvider(根);        //读取表单数据并返回一个异步任务。
        等待Request.Content.ReadAsMultipartAsync(供应商);        //这说明如何获取文件名。
        的foreach(在provider.FileData MultipartFileData文件)
        {
            Trace.WriteLine(file.Headers.ContentDisposition.FileName);
            Trace.WriteLine(服务器文件路径:+ file.LocalFileName);
        }
        返回Request.CreateResponse(的HTTPStatus code.OK);        }
        赶上(System.Exception的E)
        {
            返回Request.CreateErrorResponse(的HTTPStatus code.InternalServerError,E);
        }
    }


解决方案

我的申请正在经历这个错误太周期性。升级到WEB API 2.1什么也没做和异常消息是完全无用的。

我想什么实际发生的,虽然是有人窒息大型文件。增加在web.config中我的最大请求限制似乎解决它的权利了。

 <&的System.Web GT;
    <的httpRuntime的maxRequestLength =3000/>
< /system.web>< system.webServer>
    <安全>
      <&的requestFiltering GT;
        < requestLimits maxAllowedContentLength =3000/>
      < /&的requestFiltering GT;
    < /安全>
< /system.webServer>

(这里设置上限,以30兆,将其设置为任何你所需要的。)

I'm trying to accept a post from a client (iOS app) and my code keeps failing on reading the stream. Says the message is not complete. I've been trying to get this working for hours it seems like something is wrong in my message format or something. All I'm trying to do is read a string but the developer I'm working with who is doing the iOS part only knows how to send multipart/form-data not content-type json.

Here is exact error:

Unexpected end of MIME multipart stream. MIME multipart message is not complete."

It fails here: await Request.Content.ReadAsMultipartAsync(provider);

Headers:

POST http://localhost:8603/api/login HTTP/1.1
Host: localhost:8603
Accept-Encoding: gzip,deflate
Content-Type: multipart/form-data; boundary=------------nx-oauth216807
Content-Length: 364
Accept-Language: en-us
Accept: */*
Connection: keep-alive

Body:

--------------nx-oauth216807
Content-Disposition: form-data; name="token"

CAAH5su8bZC1IBAC3Qk4aztKzisZCd2Muc3no4BqVUycnZAFSKuleRU7V9uZCbc8DZCedYQTIFKwJbVZCANJCs4ZCZA654PgA22Nei9KiIMLsGbZBaNQugouuLNafNqIOTs9wDvD61ZA6WSTd73AVtFp9tQ1PmFGz601apUGHSimYZCjLfGBo40EBQ5z6eSMNiFeSylym1pK4PCvI17fXCmOcRix4cs96EBl8ZA1opGKVuWizOsS0WZCMiVGvT
--------------nx-oauth216807--

Here is the WebAPI code:

    public async Task<HttpResponseMessage> PostFormData()
    {
        // Check if the request contains multipart/form-data.
        if (!Request.Content.IsMimeMultipartContent())
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }
        try
        {
        string root = HttpContext.Current.Server.MapPath("~/App_Data");
        var provider = new MultipartFormDataStreamProvider(root);

        // Read the form data and return an async task.
        await Request.Content.ReadAsMultipartAsync(provider);

        // This illustrates how to get the file names.
        foreach (MultipartFileData file in provider.FileData)
        {
            Trace.WriteLine(file.Headers.ContentDisposition.FileName);
            Trace.WriteLine("Server file path: " + file.LocalFileName);
        }
        return Request.CreateResponse(HttpStatusCode.OK);

        }
        catch (System.Exception e)
        {
            return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
        }
    }

解决方案

My application was experiencing this error periodically too. Upgrading to WEB API 2.1 did nothing and the exception message is completely useless.

I think what was actually happening though is it was choking on large files. Increasing my max request limits in web.config seemed to fix it right up.

<system.web>
    <httpRuntime maxRequestLength="30000000" />
</system.web>

<system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="30000000" />
      </requestFiltering>
    </security>
</system.webServer>

(This sets the ceiling to 30 megs. Set it to whatever you need.)

这篇关于的WebAPI无法解析的multipart / form-data发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆