【问题标题】:IIS: 405 (Method not allowed) when posting to default pageIIS:发布到默认页面时出现 405(不允许的方法)
【发布时间】: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

【问题讨论】:

    标签: iis post http-status-code-405


    【解决方案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 OK
    Connection: close
    Date: Sun, 27 Jun 2010 13:28:14 GMT
    Server: Microsoft-IIS/6.0
    Location: http://errorreporting.example.com/submit/SubmitError.ashx
    Content-Length: 92
    Content-Type: text/html
    Cache-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 WritingURL 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>
      

      【讨论】:

        猜你喜欢
        • 2020-03-19
        • 2012-09-21
        • 2010-10-10
        • 2020-11-19
        • 1970-01-01
        • 2015-08-25
        • 2012-01-04
        • 2018-03-18
        • 1970-01-01
        相关资源
        最近更新 更多