pau*_*ter 5 c# iis-7 httpclient .net-4.5 asp.net-web-api
我有一个带有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);
}
....
}
Run Code Online (Sandbox Code Playgroud)
这由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)
Run Code Online (Sandbox Code Playgroud)
当在控制器的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似乎没有任何影响.
M.H*_*san 10
来自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.
Run Code Online (Sandbox Code Playgroud)
当请求筛选由于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
Run Code Online (Sandbox Code Playgroud)
对于解决最大限制问题,应配置请求筛选角色服务((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>
Run Code Online (Sandbox Code Playgroud)
对于配置详细信息审核
https://www.iis.net/configreference/system.webserver/security/requestfiltering