title: Web server HttpListener response
body: <p>I have some problems with displaying HttpListenerResponse in correct way.
  I am using <a href="http://www.codehosting.net/blog/BlogEngine/post/Simple-C-Web-Server.aspx" rel="nofollow">http://www.codehosting.net/blog/BlogEngine/post/Simple-C-Web-Server.aspx</a> as a sample! I want to display "1.html" but I can not!When I display it in my browser it display well. When I try to display as a response it displays with out image girl.png</p>
  
  <p>code of <strong>"1.html"</strong></p>
  
  <pre><code>    &lt;HTML&gt;
          &lt;BODY&gt;
              My web page.&lt;br&gt; 
              &lt;p&gt;
                &lt;img src="girl.png" width="189" height="255" alt="lorem"&gt;
                &lt;input id="btnLogin" type="button" value="login"  " style="width:100px;" /&gt;
              &lt;/p&gt;
          &lt;/BODY&gt;
      &lt;/HTML&gt;
  </code></pre>
  
  <p><strong>WebServer.cs</strong></p>
  
  <pre><code>using System;
  using System.Net;
  using System.Threading;
  using System.Linq;
  using System.Text;
  using System.IO;
  
  namespace SimpleWebServer
  {
      public class WebServer
      {
          private readonly HttpListener _listener = new HttpListener();
          private readonly Func&lt;HttpListenerRequest, string&gt; _responderMethod;
  
          public WebServer(string[] prefixes, Func&lt;HttpListenerRequest, string&gt; method)
          {
              if (!HttpListener.IsSupported)
                  throw new NotSupportedException(
                      "Needs Windows XP SP2, Server 2003 or later.");
  
              // URI prefixes are required, for example 
              // "http://localhost:8080/index/".
              if (prefixes == null || prefixes.Length == 0)
                  throw new ArgumentException("prefixes");
  
              // A responder method is required
              if (method == null)
                  throw new ArgumentException("method");
  
              foreach (string s in prefixes)
                  _listener.Prefixes.Add(s);
  
              _responderMethod = method;
              _listener.Start();
              _listener.BeginGetContext(new AsyncCallback(ListenerCallback), _listener);
          }
          private static void ListenerCallback(IAsyncResult result)
          {
              HttpListener listener = (HttpListener)result.AsyncState;
              listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);
              Console.WriteLine("New request.");
  
              HttpListenerContext context = listener.EndGetContext(result);
              HttpListenerRequest request = context.Request;
              HttpListenerResponse response = context.Response;
  
  
              byte[] page = GetFile("1.html");
  
              response.ContentLength64 = page.Length;
              Stream output = response.OutputStream;
              output.Write(page, 0, page.Length);
              output.Close();
  
  
          }
          public static byte[] GetFile(string file)
          {
              if (!File.Exists(file)) return null;
              FileStream readIn = new FileStream(file, FileMode.Open, FileAccess.Read);
              byte[] buffer = new byte[1024 * 1000];
              int nRead = readIn.Read(buffer, 0, 10240);
              int total = 0;
              while (nRead &gt; 0)
              {
                  total += nRead;
                  nRead = readIn.Read(buffer, total, 10240);
              }
              readIn.Close();
              byte[] maxresponse_complete = new byte[total];
              System.Buffer.BlockCopy(buffer, 0, maxresponse_complete, 0, total);
              return maxresponse_complete;
          }
  
          public WebServer(Func&lt;HttpListenerRequest, string&gt; method, params string[] prefixes)
              : this(prefixes, method) { }
  
          public void Run()
          {
              ThreadPool.QueueUserWorkItem((o) =&gt;
              {
                  Console.WriteLine("Webserver running...");
                  try
                  {
                      while (_listener.IsListening)
                      {
                          ThreadPool.QueueUserWorkItem((c) =&gt;
                          {
                              var ctx = c as HttpListenerContext;
                              try
                              {
                                  string rstr = _responderMethod(ctx.Request);
                                  byte[] buf = Encoding.UTF8.GetBytes(rstr);
                                  ctx.Response.ContentLength64 = buf.Length;
                                  ctx.Response.OutputStream.Write(buf, 0, buf.Length);
                              }
                              catch { } // suppress any exceptions
                              finally
                              {
                                  // always close the stream
                                  ctx.Response.OutputStream.Close();
                              }
                          }, _listener.GetContext());
                      }
                  }
                  catch { } // suppress any exceptions
              });
          }
              public void Stop()
          {
              _listener.Stop();
              _listener.Close();
          }
      }    }
  </code></pre>
  
  <p>Code in main Form</p>
  
  <pre><code>private void Form1_Load(object sender, EventArgs e)
          {
              WebServer ws = new WebServer(SendResponse, "http://localhost:8080/test/");
              ws.Run();
              Console.WriteLine("A simple webserver. Press a key to quit.");
              Console.ReadKey();
              ws.Stop();
          }
          public static string SendResponse(HttpListenerRequest request)
          {
              return string.Format("&lt;HTML&gt;&lt;BODY&gt;My web page.&lt;br&gt;{0}   &lt;p&gt;&lt;input type='submit' value='Отправить'&gt;&lt;/BODY&gt;&lt;/HTML&gt;", DateTime.Now);            }
  </code></pre>
  
  <p>How display my 1.html as in browser?</p>