2012-09-05 43 views
1

重寫HTTP體的讀取我想控制從POST讀取HTTP請求。主要讀取multipart/form-data文件上傳的流,以跟蹤從客戶端收到的流。使用.net C#HTTPHandler

使用ProcessRequest或異步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. 

    } 

} 
+0

編碼pleaseeeeee? – VIRA

回答

-1

你絕對可以做到這一點的添加代碼示例實現一個IHttpHandler。

這個example會讓你開始。沒有必要重寫內置的讀數。
您會收到請求中的所有數據,並且可以根據需要對其進行處理。

+2

在該示例中,我看不到任何參考文獻是直接從客戶端讀取文本體。 這與任何其他HttpHander沒有什麼不同,'InputStream'已經緩存了'POST'的主體。我想從客戶端讀取實際的流,不允許IIS/.Net讀取正文。 – hokapoka

1

看來您可以通過HttpModule的context.BeginRequest事件捕獲流。

例如:

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的做。