调试期间记录HttpListener响应OutputStream

时间:2018-09-25 10:05:37

标签: c# reflection outputstream httplistener

我尝试记录我的HttpListener的每个请求和响应。

对于HttpListenerContext.Request.InputStream,这很简单:

var finfoIn = context.Request.GetType().GetField("m_RequestStream", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
context.Request.InputStream.CopyTo(memstreamIn);
memstreamIn.Seek(0, System.IO.SeekOrigin.Begin);
var reader = new System.IO.StreamReader(memstreamIn, Encoding.Unicode);
this.Log(reader.ReadToEnd()); //Actual log method that prints to console
memstreamIn.Seek(0, System.IO.SeekOrigin.Begin);
finfoIn.SetValue(context.Request, memstreamIn);

但是,由于HttpListenerContext.Response.OutputStream的字段类型为(internal) System.Net.HttpResponseStream,因此上述技巧是不可能的。

有人能找到允许登录OutputStream的解决方案吗?

1 个答案:

答案 0 :(得分:-1)

这是一个非常简单的调试示例:

Please check following settings
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="AngularJS" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="/" />
        </rule>
      </rules>
    </rewrite>
    <staticContent>
        <remove fileExtension=".json"/>
        <remove fileExtension=".woff"/>
        <remove fileExtension=".woff2"/>
        <remove fileExtension=".svg"/>
        <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
        <mimeMap fileExtension=".json" mimeType="application/json" />
        <mimeMap fileExtension=".woff" mimeType="application/x-font-woff" />
        <mimeMap fileExtension=".woff2" mimeType="font/woff2" />          
    </staticContent>
  </system.webServer>
</configuration>