2017-06-16 45 views
1

我已经使用http侦听器设置了一个Web服务器。它用来显示一个html文件。该html文件创建了一个我希望应用程序获得的请求。这里是小提琴手请求的截图: This is the only request that needs to be captured使用http侦听器获取端口上的Web流量

这是我的尝试:

var rstr = _responderMethod(ctx.Request); 
var buf = Encoding.UTF8.GetBytes(rstr); 
ctx.Response.ContentLength64 = buf.Length; 
ctx.Response.OutputStream.Write(buf, 0, buf.Length); 
Console.Write(ctx.Response.OutputStream); 

该代码总是返回“System.Net.HttpResponseStream”每一个请求是在服务器上进行的时间。我想看看它是什么回来,但作为一个字符串。

这里是我的Web服务器代码:

public class WebServer 
{ 
private readonly HttpListener _listener = new HttpListener(); 
private readonly Func<HttpListenerRequest, string> _responderMethod; 

    public WebServer(IReadOnlyCollection<string> prefixes, Func<HttpListenerRequest, string> method) 
    { 
     if (!HttpListener.IsSupported) 
     { 
      throw new NotSupportedException("Needs Windows XP SP2, Server 2003 or later."); 
     } 

     // URI prefixes are required eg: "http://localhost:8080/test/" 
     if (prefixes == null || prefixes.Count == 0) 
     { 
      throw new ArgumentException("URI prefixes are required"); 
     } 

     if (method == null) 
     { 
      throw new ArgumentException("responder method required"); 
     } 

     foreach (var s in prefixes) 
     { 
      _listener.Prefixes.Add(s); 
     } 

     _responderMethod = method; 
     _listener.Start(); 
    } 

    public WebServer(Func<HttpListenerRequest, string> method, params string[] prefixes) 
     : this(prefixes, method) 
    { 
    } 

    public void Run() 
    { 
     ThreadPool.QueueUserWorkItem(o => 
     { 
      Console.WriteLine("Webserver running..."); 
      try 
      { 
       while (_listener.IsListening) 
       { 
        ThreadPool.QueueUserWorkItem(c => 
        { 
         var ctx = c as HttpListenerContext; 
         try 
         { 
          if (ctx == null) 
          { 
           return; 
          } 

          var rstr = _responderMethod(ctx.Request); 
          var buf = Encoding.UTF8.GetBytes(rstr); 
          ctx.Response.ContentLength64 = buf.Length; 
          ctx.Response.OutputStream.Write(buf, 0, buf.Length); 

         } 
         catch 
         { 
          // ignored 
         } 
         finally 
         { 
          // always close the stream 
          if (ctx != null) 
          { 
           ctx.Response.OutputStream.Close(); 
          } 
         } 
        }, _listener.GetContext()); 
       } 
      } 
      catch (Exception ex) 
      { 
       // ignored 
      } 
     }); 

    } 

    public void Stop() 
    { 
     _listener.Stop(); 
     _listener.Close(); 
    } 

} 
} 
+0

可以试试这个Console.Write(buf); –

+0

[请勿将标记置于问题标题中](https://stackoverflow.com/help/tagging) – Liam

回答

0

从你提供的,它看起来像你写rstr为UTF8字符串输出流的代码,所以当你阅读输出流再次得到什么它返回你会得到相同的价值rstr。所以这样做应该打印你在stdout/console中发回的内容。

Console.WriteLine(rstr); 

,或者您可以使用StreamReader类来包装输出流并作为字符串读取数据。

using (var reader = new StreamReader(ctx.Response.OutputStream) 
{ 
    // Seek to origin, to read all data in the output stream, 
    ctx.Response.OutputStream.Seek(0, SeekOrigin.Begin); 
    var content = reader.ReadToEnd(); 
    Console.WriteLine(content); 

    // You might not want to close the output stream in case you want to do more work with it. 
} 
+0

第一种方法只是返回完整的html文件。 – Andre

+0

由于很多错误,我无法获得第二个工作。这是因为我正在做很多事情。我会用我所拥有的更新我的问题 – Andre