gpt4 book ai didi

c# - 可以作为参数传递给 POST 方法的对象的最大大小

转载 作者:太空狗 更新时间:2023-10-29 22:25:38 25 4
gpt4 key购买 nike

我有一个带有 POST 方法的 Web API Controller ,如下所示。

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)

当 Controller 的 POST 方法参数中传递的 MyObject 对象大小较小时,效果很好。但是,如果此对象大小很大,我会在 POST 方法参数中为请求对象获取一个空值。在一种情况下,从客户端请求传递的 requestClient 对象的大小约为 5 MB,在 POST 方法中,我将请求对象设为 null。 请注意,Web API 托管在 IIS 下。是否有任何设置需要更改以获得允许的大小。

更新:在 web.config 中添加以下内容解决了 POST 方法参数中的空对象问题。

httpRuntime maxRequestLength="2147483647" />

然后我将 requestClient 对象的大小增加到 ~50MB。现在 POST 方法中的代码永远不会被命中。在客户端,在调用 PostAsJsonAsyn 时,我收到带有以下消息的 System.Net.HttpRequestException。

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

现在更改 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。配置

    <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

关于c# - 可以作为参数传递给 POST 方法的对象的最大大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38959442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com