2009-05-18 27 views
10

我正在使用ASP.NET MVC實現一個REST API,並且有一個小絆腳石出現在請求頭的Expect: 100-continue請求頭的形式一個帖子主體。支持ASP.NET MVC中的「Expect:100-continue」標頭

RFC 2616指出:

Upon receiving a request which includes an Expect request-header field with the "100-continue" expectation, an origin server MUST either respond with 100 (Continue) status and continue to read from the input stream, or respond with a final status code. The origin server MUST NOT wait for the request body before sending the 100 (Continue) response. If it responds with a final status code, it MAY close the transport connection or it MAY continue to read and discard the rest of the request. It MUST NOT perform the requested method if it returns a final status code.

這聽起來好像我需要做響應的要求,即它需要立即發送一個HTTP 100繼續響應,然後繼續從閱讀原始請求流(即HttpContext.Request.InputStream),而不結束請求,然後最終發送結果狀態碼(爲了參數,可以說它是204無內容結果)。

所以,問題是:

  1. 上午我在閱讀說明書正確的,我需要做兩個響應的請求?
  2. 這怎麼能在ASP.NET MVC中完成?

w.r.t. (2)我已經在進行讀取輸入流之前使用下面的代碼試圖...

HttpContext.Response.StatusCode = 100; 
HttpContext.Response.Flush(); 
HttpContext.Response.Clear(); 

...但是當我嘗試設置最後的204個狀態碼我得到的錯誤:

System.Web.HttpException: Server cannot set status after HTTP headers have been sent.

回答

2

100-continue應該由IIS處理。爲什麼你想明確地做到這一點有什麼原因嗎?

+0

不 - 我想避免它!我沒有意識到IIS會在沒有任何干預的情況下處理它。 – 2009-05-19 08:17:24

+0

我在`WebRequest`中遇到了100個繼續的錯誤。這是一個不使用它的好理由。 http://regis.decamps.info/blog/2010/12/c-bug-in-webrequest/ – rds 2010-12-28 11:13:02

2

IIS處理100.

這就是說,不,它不是兩個響應。在HTTP中,當Expect:100-continue作爲消息頭的一部分進入時,客戶端應該等待,直到它在發送內容之前收到響應。

由於asp.net的構建方式,您幾乎無法控制輸出流。無論何時刷新,任何寫入流的數據都會自動放入200響應中,分塊編碼,無論是否處於緩衝模式。

不幸的是,所有這些東西都隱藏在各地的內部方法中,結果是,如果你依賴於asp.net,就像MVC一樣,你幾乎無法繞過它。

等到您嘗試以非緩衝方式訪問輸入流時。一整天的痛苦。

的Seb

15

默認NET框架始終發送expect: 100-continue標題爲每個HTTP 1.1交。這種行爲可以被每個請求通過System.Net.ServicePoint.Expect100Continue屬性編程控制,像這樣:

HttpWebRequest httpReq = GetHttpWebRequestForPost(); 
httpReq.ServicePoint.Expect100Continue = false; 

它也可以被編程全局控制:

...或通過全球配置:

<system.net> 
    <settings> 
    <servicePointManager expect100Continue="false"/> 
    </settings> 
</system.net> 

謝謝蘭斯Olson和Phil Haack此信息。