Come accedere richiesta InputStream con HttpModule, quindi reimpostare la posizione InputStream
-
16-09-2019 - |
Domanda
Sto cercando di registrare il contenuto di una richiesta HTTP, utilizzando un IHttpModule in questo modo:
public class LoggingModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += ContextBeginRequest;
}
private void ContextBeginRequest(object sender, EventArgs e)
{
var request = ((HttpApplication)sender).Request;
string content;
using (var reader = new StreamReader(request.InputStream))
{
content = reader.ReadToEnd();
}
LogRequest(content)
}
}
Il problema è che dopo aver letto il flusso di input fino alla fine, l'InputStream sembra avere sia scomparso o più probabilmente, il cursore si trova alla fine del flusso.
Ho provato request.InputStream.Position = 0;
e request.InputStream.Seek(0, SeekOrigin.Begin);
ma nessuno dei due lavori.
Soluzione
Ho lavorato il problema: penso che chiamando Dispose sul StreamReader deve essere uccidendo l'InputStream troppo
.Invece di usare lo StreamReader ho fatto la seguente:
var bytes = new byte[request.InputStream.Length];
request.InputStream.Read(bytes, 0, bytes.Length);
request.InputStream.Position = 0;
string content = Encoding.ASCII.GetString(bytes);
Quindi, il codice completo:
public class LoggingModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += ContextBeginRequest;
}
private void ContextBeginRequest(object sender, EventArgs e)
{
var request = ((HttpApplication)sender).Request;
var bytes = new byte[request.InputStream.Length];
request.InputStream.Read(bytes, 0, bytes.Length);
request.InputStream.Position = 0;
string content = Encoding.ASCII.GetString(bytes);
LogRequest(content)
}
}
Altri suggerimenti
Sì, la StreamReader si chiuderà il flusso in dotazione.
Se sei in> v4.5, utilizzare un costruttore StreamReader che lascia il flusso aperto.
using (var reader = new StreamReader(request.InputStream, Encoding.UTF8, true, 1024, true))
{
content = reader.ReadToEnd();
}
Ho dovuto fare un piccolo tweak per la risposta fornita da "CBP". Quando si utilizza il suo codice Ho appena ricevuto zeri. Ho spostato l'impostazione della posizione a 0 al di sopra della lettura e ora funziona.
var bytes = new byte[Request.InputStream.Length];
Request.InputStream.Position = 0;
Request.InputStream.Read(bytes, 0, bytes.Length);
string content = Encoding.ASCII.GetString(bytes);
È necessario utilizzare un filtro richiesta . Scrivi una classe derivante da streaming e registrarlo come un filtro.
questa risposta non ha funzionato. restituisce un array che contiene i valori null.
var bytes = new byte[request.InputStream.Length]; request.InputStream.Read(bytes, 0, bytes.Length); request.InputStream.Position = 0; string content = Encoding.ASCII.GetString(bytes);
perché il flusso di input consumato.
a volte, RequestFilter
non correre al metodo Read. E sembra BE W3WP non leggere il contenuto di httprequest
dalla via normale.
Se si distribuisce WEbservice
al server. Quindi utilizzare IHttpModule per prenderlo. Aggiungere RequestFilter
.
Ma il metodo Read()
di RequestFilter non correre: P