IIS:发布到默认页面时出现 405(不允许的方法)

Posted

技术标签:

【中文标题】IIS:发布到默认页面时出现 405(不允许的方法)【英文标题】:IIS: 405 (Method not allowed) when posting to default page 【发布时间】:2011-03-08 09:32:50 【问题描述】:

我想POST 将数据发送到网络服务器的默认页面,例如:

POST http://errorreporting.example.com/ HTTP/1.1

我希望服务器负责 302 将客户端重定向到 POST 应该去的地方。服务器上的default.asp 文件通过执行Redirect 来执行此任务(which is the technique Microsoft recommends):

default.asp:

<%
   Response.Redirect "SubmitError.ashx"
%>

当我简单地浏览服务器时:

GET http://errorreporting.example.com/ HTTP/1.1

我从服务器得到了预期的响应:

HTTP/1.1 302 Object moved
Server: Microsoft-IIS/5.0
Location: SubmitError.ashx
...

但是当我POST到服务器时:

POST http://errorreporting.example.com/ HTTP/1.1

服务器对我很生气:

HTTP/1.1 405 Method not allowed
Connection: close
Allow: OPTIONS, TRACE, GET, HEAD
...

我希望服务器能够将客户端重定向到适当的提交URL,而不是使用URL。当然,这是因为 URL 可能(即已经)改变了:

http://errorreporting.example.com/SubmitError.asp http://errorreporting.example.com/SubmitError.aspx http://errorreporting.example.com/SubmitError.ashx http://errorreporting.example.com/ErrorReporting/Submit.ashx http://errorreporting.example.com/submiterror.php http://errorreporting.example.com/submit/submiterror.ashx

等等

注意:如果我将URL 更改为包含文档位置:

/* SErrorReportingUrl = 'http://www.example.com:8088/ErrorReporting/SubmitError.ashx';
   SErrorReportingUrl = 'http://www.example.com/ErrorReporting/SubmitError.ashx';
   SErrorReportingUrl = 'http://errorreporting.example.com:8088/ErrorReporting/SubmitError.ashx';
   SErrorReportingUrl = 'http://errorreporting.example.com/SubmitError.ashx';
   SErrorReportingUrl = 'http://errorreporting.example.com';
   SErrorReportingUrl = 'http://errorreporting.example.com/SubmitError.ashx'; 
*/
   SErrorReportingUrl = 'http://errorreporting.example.com/submit/SubmitError.ashx';

效果很好:

HTTP/1.1 200 OK

【问题讨论】:

【参考方案1】:

结果证明让 IIS 支持 POST 并没有真正的帮助。

我正在使用XMLHttpRequest 来执行POSTs。几乎所有XMLHttpRequest 的实现(例如 Chrome、Internet Explorer、MSXML)都有一个错误,在重定向之后它执行GET,而不是POST。 p>

W3C's XMLHttpReqest specification 表示应遵循重定向并重新发出请求:

如果响应是 HTTP 重定向:

如果重定向不违反安全性 (例如same origin), 无限循环预防措施,以及 透明地支持方案 在观察时遵循重定向 same-origin request event rules。

注意:HTTP 对用户提出了要求 关于保存的代理 request methodrequest entity body 在重定向期间,以及 需要通知最终用户 某些类型的自动 重定向。

还有here's a site,您可以在其中测试浏览器的XmlHttpRequest 实现中的错误。


最后,我将解决 IIS 错误和 XMLHttpRequest 错误,方法是让我的“默认”页面执行 302 redirect 所做的所有事情,但返回 @987654336 @ 改为:

HTTP/1.1200 OKConnection: closeDate: Sun, 27 Jun 2010 13:28:14 GMTServer: Microsoft-IIS/6.0Location: http://errorreporting.example.com/submit/SubmitError.ashxContent-Length: 92Content-Type: text/htmlCache-control: private

<HTML><BODY>This content has moved <A href="submit/SubmitError.ashx">here.</A></BODY></HTML>

无论如何,我都在强迫客户进行两次点击 - 还不如让它正常工作。

【讨论】:

【参考方案2】:

我在 Express/Node 上运行 Vue.js。后端在服务器端口 3300 上运行,但我们不想公开它。

我安装了需求(Application Request Writing 和URL Rewrite),然后添加了一个入站规则来重写对 Express 的后端请求,但我一直收到 405(不允许的方法)。

最终,我从Stefano Scerra's Blog 收集了一些想法。

这是我最终的 web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Handle History Mode and custom 404/500" stopProcessing="true">
          <match url="(.*)" />
          <conditions logicalGrouping="MatchAll">
            <add input="REQUEST_FILENAME" matchType="IsFile" negate="true" />
            <add input="REQUEST_FILENAME" matchType="IsDirectory" negate="true" />
            <add input="REQUEST_URI" pattern="^/(api)" negate="true"/>
          </conditions>
          <action type="Rewrite" url="/" />
        </rule>
        <rule name="api" stopProcessing="true">
            <match url="^api/(.*)$" />
            <action type="Rewrite" url="http://10.1.1.217:3300/R:1" logRewrittenUrl="true" />
        </rule>
      </rules>
    </rewrite>
        <tracing>
            <traceFailedRequests>
                <add path="*">
                    <traceAreas>
                        <add provider="WWW Server" areas="Filter" verbosity="Verbose" />
                    </traceAreas>
                    <failureDefinitions timeTaken="00:00:00" statusCodes="100-999" />
                </add>
            </traceFailedRequests>
        </tracing>
  </system.webServer>
</configuration>

【讨论】:

以上是关于IIS:发布到默认页面时出现 405(不允许的方法)的主要内容,如果未能解决你的问题,请参考以下文章

尝试使用 blueimp jquery 上传小部件上传时出现 405(不允许的方法)错误

在 Laravel 5.2 上路由文件路径时出现 405(不允许的方法)

jQuery-File-upload:删除时出现 405 错误“方法不允许”

405 - 不允许用于访问此页面的 HTTP 动词。 [IIS 8.5] [Windows Server 2012 R2]

405 - 用于访问此页面的 http 动词在 reactjs 应用程序中不允许 iis

使用 IIS 时方法不允许 405