簡體   English   中英

可以作為參數傳遞給POST方法的對象的最大大小

[英]The maximum size of object that can be passed as Parameter to POST method

我有一個帶有POST方法的Web API控制器,如下所示。

public class MyController : ApiController
{
    // POST: api/Scoring
    public HttpResponseMessage Post([FromBody]MyClass request)
    {
        // some processing of request object
        return Request.CreateResponse(HttpStatusCode.OK, someResponseObject);
    }
    ....
}

這由HTTPClient消耗如下

HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.BaseAddress = new Uri("http://localhost");
MyClass requestClient = new MyClass();
var task = httpClient.PostAsJsonAsync("api/my", requestClient)

當在控制器的POST方法參數中傳遞的MyObject對象大小很小時,它工作得很好。 但是,如果此對象大小很大,我在POST方法參數中獲取請求對象的null。 在一種情況下,從客戶端請求傳遞的requestClient對象的大小約為5 MB,在POST方法中,我將請求對象作為null。 請注意,Web API托管在IIS下。 我是否需要根據允許的尺寸更改設置。

更新:在web.config中添加以下內容解決了POST方法參數中的空對象問題。

httpRuntime maxRequestLength =“2147483647”/>

然后我將requestClient對象的大小增加到~50MB。 現在POST方法中的代碼永遠不會受到攻擊。 在客戶端,在調用PostAsJsonAsyn時,我得到System.Net.HttpRequestException並帶有以下消息。

響應狀態代碼不表示成功:404(未找到)。

現在改變maxRequestLength似乎沒有任何影響。

來自OP:

> then increased the size of requestClient object to ~50MB. Now the code in POST method never getting hit. On the client side, at the call to PostAsJsonAsyn, I get System.Net.HttpRequestException with following message.

Response status code does not indicate success: 404 (Not Found).

Now changing maxRequestLength doesn’t seem to have any impact.

當請求篩選由於HTTP請求超出請求限制而阻止HTTP請求時,IIS將向客戶端返回HTTP 404錯誤,並使用唯一的子狀態記錄以下HTTP狀態之一,該子狀態標識拒絕請求的原因:

 HTTP Substatus      Description
 404.13                 Content Length Too Large
 404.14                 URL Too Long
 404.15                 Query String Too Long
 ..etc

對於解決最大限制問題,應配置請求篩選角色服務((IIS)7.0及更高版本中引入的內置安全功能):通過SERVER MANAGER GUI或命令實用程序appcmd.exe或修改Web.config

    <configuration>
       <system.webServer>
          <security>
             <requestFiltering>
                 .....

                <!-- limit post size to 10mb, query string to 256 chars, url to 1024 chars -->
                <requestLimits maxQueryString="256" maxUrl="1024" maxAllowedContentLength="102400000" />

                .....
             </requestFiltering>
          </security>
       </system.webServer>
    </configuration>

對於配置詳細信息審核

https://www.iis.net/configreference/system.webserver/security/requestfiltering

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM