如何让IIS 10.0的http.sys允许空内容POST请求?
解决WOPI客户端空内容POST请求触发http.sys 411错误的方案
针对Windows Server 2019 Version 1809 + IIS 10.0.17763.1环境下,WOPI客户端发送空内容POST请求被http.sys返回411 Length Required错误的问题,可通过以下服务器端配置解决,无需修改客户端代码:
1. 全局修改http.sys配置(适用于所有站点)
http.sys默认强制POST请求携带Content-Length头,可通过注册表禁用该检查:
- 打开注册表编辑器,定位到
HKLM\System\CurrentControlSet\Services\HTTP\Parameters - 新建DWORD(32位)值,命名为
AllowZeroContentLength,设置值为1 - 重启HTTP服务(执行命令
net stop http && net start http,注意此操作会重启所有依赖HTTP的服务,如IIS)
2. 针对WOPI路径配置URL Rewrite(更安全,仅影响WOPI请求)
通过IIS URL Rewrite模块为空内容POST请求添加Content-Length: 0头:
- 确保已安装IIS URL Rewrite模块
- 在WOPI Host站点的
web.config中添加以下规则(替换/wopi/.*为你的实际WOPI请求路径):
<configuration> <system.webServer> <rewrite> <outboundRules> <rule name="Add Content-Length for empty WOPI POST" preCondition="Missing Content-Length"> <match serverVariable="RESPONSE_Content-Length" pattern=".*" /> <action type="Rewrite" value="0" /> <conditions> <add input="{REQUEST_METHOD}" pattern="POST" /> <add input="{REQUEST_URI}" pattern="/wopi/.*" /> </conditions> </rule> </outboundRules> <preConditions> <preCondition name="Missing Content-Length"> <add input="{REQUEST_CONTENT_LENGTH}" pattern="^$" /> </preCondition> </preConditions> </rewrite> </system.webServer> </configuration>
3. 在WOPI Host代码中处理(适用于自定义开发的WOPI Host)
如果你的WOPI Host是基于ASP.NET(Core/Framework)开发的,可在请求处理管道中提前处理空内容POST:
- ASP.NET Core:添加中间件检查请求方法为POST且ContentLength为null时,设置
Request.ContentLength = 0; - ASP.NET Framework:在Global.asax的
BeginRequest事件中做类似处理
以上方案均无需针对每个WOPI客户端单独配置,服务器端配置完成后即可正常接收空内容POST请求。
内容的提问来源于stack exchange,提问作者0lli.rocks
相关产品推荐
相关产品推荐

