2017-06-16 2 views
1

httpリスナーを使用してWebサーバーをセットアップしました。これは、htmlファイルを表示するために使用されます。 htmlファイルは、アプリケーションが取得したい要求を作成します。ここではシオマネキにおける要求のスクリーンショットです: This is the only request that needs to be capturedhttpリスナーを使用してポート上で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の要求がサーバー上で行われるたびに返されます。私はそれが何を返すのかを文字列として見たい。

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

[質問タイトルにタグを入れないでください] – Liam

答えて

0

あなたは出力ストリームにUTF8文字列としてrstrを書いているようなので、あなたが得るために再び出力ストリームを読んだとき、それは見え提供されているコードから:ここで

は私のウェブサーバーのコードです返すものは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

エラーが多いため、2番目のエラーが発生することはありません。それは私が多分多くのことの中でそれをやっているからかもしれない。私は私が持っているもので私の質問を更新する – Andre

関連する問題