HttpListener - Come faccio a inviare un WebException HTTP 304 “Not Modified” di nuovo l'errore al browser?
-
25-09-2019 - |
Domanda
Come faccio a simulare un WebException 304 errore al browser, se sto usando HttpListener?
Questo è il Ho ricevuto una richiesta alla mia HttpListener, e poi ottenuto il HttpListenerContext, quindi da questo punto come faccio a mimare / organizzare un HTTP "304 Not Modified" risposta da inviare in modo efficace al browser tramite il HttpListenerContext .risposta?
EDIT:
Ho provato quanto segue tuttavia ottengo un errore nel tentativo di copiare WebException.Status a HttpWebResponse.StatusCode (Il codice di stato deve essere esattamente tre cifre). Tutte le idee su come correggere questo?
catch (WebException ex)
{
listenerContext.Response.StatusCode = (int)ex.Status; //ERROR: The status code must be exactly three digits
listenerContext.Response.StatusDescription = ex.Message;
listenerContext.Response.Close();
grazie
Soluzione
Credo di avere con:
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.ProtocolError)
{
int statusCode = (int) ((HttpWebResponse) ex.Response).StatusCode;
listenerContext.Response.StatusCode = statusCode;
listenerContext.Response.StatusDescription = ex.Message;
log("WARNING", uri, "WebException/ProtocolError: " + ex.GetType() + " - " + ex.Message);
}
else
{
log("ERROR", uri, "WebException - " + ex.GetType() + " - " + ex.Message);
}
listenerContext.Response.Close();
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow