Sto implementando un'API REST utilizzando ASP.NET MVC e un piccolo ostacolo è arrivato sotto forma dell'intestazione della richiesta Expect: 100-continue
per le richieste con un corpo postale.Supporto dell'intestazione "Expect: 100-continue" con ASP.NET MVC
RFC 2616 afferma che:
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.
Questo suona per me come ho bisogno di fare due risposte alla richiesta, vale a dire che deve inviare immediatamente un HTTP 100 Continue risposta, e poi continuare a leggere dalla flusso di richiesta originale (ovvero HttpContext.Request.InputStream
) senza terminare la richiesta, quindi inviare il codice di stato risultante (per ragioni di discussione, diciamo che è un risultato 204 senza contenuto).
Quindi, le domande sono:
- Sto leggendo le specifiche destra, che ho bisogno di fare due risposte ad una richiesta?
- Come può essere fatto in ASP.NET MVC?
w.r.t. (2) Ho provato con il seguente codice prima di procedere alla lettura del flusso di input ...
HttpContext.Response.StatusCode = 100;
HttpContext.Response.Flush();
HttpContext.Response.Clear();
... ma quando provo a impostare il codice 204 di stato finale ottengo l'errore:
System.Web.HttpException: Server cannot set status after HTTP headers have been sent.
No, mi piacerebbe evitarlo! Non avevo realizzato che IIS lo avrebbe gestito senza alcun intervento. –
Ho riscontrato un errore in 'WebRequest' con 100 Continua. Questa è una buona ragione per non usarla. http://regis.decamps.info/blog/2010/12/c-bug-in-webrequest/ – rds