1

C# を使用して、POST からの HTTP 要求の読み取りを制御したいと考えています。主に、ファイル アップロードのストリームを読み取り、multipart/form-dataクライアントから受信したストリームを追跡します。

ProcessRequestまたは Asyncを使用するとBeginProcessRequest、本文は ASP.net / IIS によって既に解析されています。

HTTPHandler を介して組み込みの読み取りをオーバーライドする方法はありますか、それとも別のメカニズムを使用する必要がありますか?

どうもありがとう

アンディ

更新- IHttpHandler を実装した通常のクラスと変わらないが、要求に応じてコード例を追加

public class MyHandler : IHttpHandler
{

    public bool IsReusable { get { return true; } }

    public void ProcessRequest(HttpContext context)
    {
        // The body has already been received by the server
        // at this point.  

        // I need a way to access the stream being passed 
        // from the Client directly, before the client starts 
        // to actually send the Body of the Request.

    }

}
4

2 に答える 2

1

context.BeginRequestHttpModuleのイベントを介してストリームをキャプチャできるようです。

例えば ​​:

public class Test : IHttpModule
{

    public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(onBeginRequest);
    }


    public void onBeginRequest(object sender, EventArgs e)
    {
        HttpContext context = (sender as HttpApplication).Context;
        if( context == nul ) { return; }

        if (context.Request.RawUrl.Contains("test-handler.ext"))
        {
            Logger.SysLog("onBeginRequest");
            TestRead(context);
        }

    }

    // Read the stream
    private static void TestRead(HttpContext context)
    {
        using (StreamReader reader = new StreamReader(context.Request.GetBufferlessInputStream()))
        {
            Logger.SysLog("Start Read");
            reader.ReadToEnd();
            Logger.SysLog("Read Completed");
        }
    }
}

本当に私はHttpModulesを避けようとしていました.netリクエストごとに処理されるので、HTTPHandlerを介してそれを行う方法を知りたいです。

于 2012-09-05T10:48:01.253 に答える
-1

IHttpHandler を実装することで、確実にそれを行うことができます。

このから始めることができます。組み込みの読み値をオーバーライドする必要はありません。
リクエスト内のすべてのデータを受け取り、必要に応じて処理できます。

于 2012-09-05T10:09:09.390 に答える