2017-06-16 96 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