2009-11-05 11 views
23

मैं एक http अनुरोध की सामग्री को लॉग इन करने की कोशिश कर रहा हूँ रीसेट, तो जैसे एक IHttpModule का उपयोग कर:प्रवेश कैसे करें HttpModule साथ अनुरोध InputStream, तो InputStream स्थिति

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) 
    } 
} 

समस्या यह है कि करने के लिए इनपुट धारा को पढ़ने के बाद अंत में, इनपुटस्ट्रीम या तो गायब हो गया है या अधिक संभावना है, कर्सर स्ट्रीम के अंत में है।

मैंने request.InputStream.Position = 0; और request.InputStream.Seek(0, SeekOrigin.Begin); पर प्रयास किया है लेकिन न तो काम करता है।

उत्तर

31

मैंने समस्या का समाधान किया है: मुझे लगता है कि StreamReader पर निपटने का आह्वान इनपुटस्ट्रीम को भी मारना चाहिए।

इसके बजाय StreamReader मैं निम्नलिखित किया था का उपयोग करने का

:

 var bytes = new byte[request.InputStream.Length]; 
     request.InputStream.Read(bytes, 0, bytes.Length); 
     request.InputStream.Position = 0; 
     string content = Encoding.ASCII.GetString(bytes); 

तो पूरा कोड:

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) 
    } 
} 
+6

वहां एन्कोडिंग से सावधान रहें, मुझे लगता है कि आप इसे एन्कोडिंग करना चाहते हैं .UTF8.GetString (बाइट्स); – SimonF

+0

महान जवाब !! +1 –

1

आपको request filter का उपयोग करने की आवश्यकता है। Stream से प्राप्त कक्षा लिखें और इसे फ़िल्टर के रूप में पंजीकृत करें।

1

इस उत्तर काम नहीं किया। यह एक सरणी देता है जिसमें शून्य मान होते हैं।

 
     var bytes = new byte[request.InputStream.Length]; 
     request.InputStream.Read(bytes, 0, bytes.Length); 
     request.InputStream.Position = 0; 
     string content = Encoding.ASCII.GetString(bytes);

क्योंकि इनपुट स्ट्रीम खपत हुई।

-1

कभी-कभी, RequestFilter विधि पढ़ने के लिए नहीं चलाएं। ऐसा लगता है कि W3WP सामान्य रूप से httprequest की सामग्री नहीं पढ़ता है।

यदि आप सर्वर पर WEbservice तैनात करते हैं। फिर इसे पकड़ने के लिए IHttpModule का उपयोग करें। RequestFilter जोड़ें।

लेकिन RequestFilter की विधि Read() नहीं चला: पी

13

हाँ StreamReader आपूर्ति की धारा बंद हो जाएगा।

यदि आप> v4.5 पर हैं, तो StreamReader कन्स्ट्रक्टर का उपयोग करें जो स्ट्रीम को खोल देता है।

using (var reader = new StreamReader(request.InputStream, Encoding.UTF8, true, 1024, true)) 
{ 
    content = reader.ReadToEnd(); 
} 
1

मुझे "सीबीपी" द्वारा प्रदान किए गए उत्तर में एक छोटा सा बदलाव करना पड़ा। अपने कोड का उपयोग करते समय मुझे शून्य मिल गया। मैंने स्थिति को ऊपर से 0 पर सेट करने के लिए स्थानांतरित कर दिया और अब यह काम करता है।

var bytes = new byte[Request.InputStream.Length]; 
Request.InputStream.Position = 0; 
Request.InputStream.Read(bytes, 0, bytes.Length); 
string content = Encoding.ASCII.GetString(bytes);