title: C# using HttpListener and Request.ServerVariables on Windows Forms or Console
body: <p><strong>Project Objectives:</strong><br><br>
  Create a local Proxy Judge using a Console or Windows Form application for debugging and testing connections.<br></p>
  
  <ol>
  <li>Project must request and receive proxy ServerVariables to display on client side.</li>
  <li>Parse IPAddress and return Anonymity state. </li>
  <li>Implement Basic Athentifcation Scheme.</li>
  <li>Project must not use Scripts for functionality (e.g) PHP, Perl, Asp, etc.</li>
  <li>Multi-platform Compatible (possibilty)</li>
  </ol>
  
  <p><img src="https://i.stack.imgur.com/p85yI.png" alt="alt text"></p>
  
  <hr>
  
  <p><strong>Questions:</strong></p>
  
  <ol>
  <li><p>Is it possible to use Request.ServerVariables on a local Windows or Console Application or is it ASP specific?</p></li>
  <li><p>If this method is ASP specific is there another way to request the ServerVariables from a browser session?</p></li>
  <li><p>If the method above is possible what is the proper approach for achieving this functionality?</p></li>
  <li><p>What is a good example for verifing/Setting the Basic Authentification Scheme here? Like setting the password and user to be used and so on.</p></li>
  </ol>
  
  <hr>
  
  <p><strong>References used:</strong><br><br>
  <a href="http://msdn.microsoft.com/en-us/library/system.web.httpapplication.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/system.web.httpapplication.aspx</a>
  <a href="http://www.java2s.com/Code/CSharpAPI/System.Net/HttpListenerContextResponseStatusCode.htm" rel="nofollow noreferrer">http://www.java2s.com/Code/CSharpAPI/System.Net/HttpListenerContextResponseStatusCode.htm</a>
  <a href="http://en.cship.org/wiki/ProxyJudge" rel="nofollow noreferrer">http://en.cship.org/wiki/ProxyJudge</a></p>
  
  <p><strong>Example Code:</strong></p>
  
  <pre><code>using System.IO;
  using System.Net;
  using System.Web;
  using System.Collections.Specialized;
  
  namespace IPJudge
  {
      public class IPJudgeClass : IHttpModule
      {
          public static void Main()
          {
              using (HttpListener listener = new HttpListener())
              {
                  listener.AuthenticationSchemes = AuthenticationSchemes.None;
                  listener.Prefixes.Add("http://localhost:8080/");
                  //listener.Prefixes.Add("https://localhost/");
                  listener.Start();
  
                  HttpListenerContext ctx = listener.GetContext();
                  ctx.Response.StatusCode = 200;
                  string name = ctx.Request.QueryString["name"];
  
                  StreamWriter writer = new StreamWriter(ctx.Response.OutputStream);
                  writer.WriteLine("&lt;P&gt;Hello, {0}&lt;/P&gt;", name);
                  writer.WriteLine("&lt;ul&gt;");
                  foreach (string header in ctx.Request.Headers.Keys)
                  {
                      writer.WriteLine("&lt;li&gt;&lt;b&gt;{0}:&lt;/b&gt; {1}&lt;/li&gt;", header, ctx.Request.Headers[header]);
                  }
                  writer.WriteLine("&lt;/ul&gt;");
  
                  writer.Close();
                  ctx.Response.Close();
                  listener.Stop();
              }
          }
  
          public void Init(HttpApplication app)
          {
  
              app.AcquireRequestState += new System.EventHandler(app_AcquireRequestState);
              app.PostAcquireRequestState += new System.EventHandler(app_PostAcquireRequestState);
          }
  
          public void app_AcquireRequestState(object o, System.EventArgs e)
          {
              HttpApplication httpApp = (HttpApplication)o;
              HttpContext ctx = HttpContext.Current;
              ctx.Response.Write(" Executing AcquireRequestState ");
              ctx.Response.Close();
          }
  
          public void Dispose()
          {
              // TODO:
              // Add code to clean up the
              // instance variables of a module.
          }
  
          public void app_PostAcquireRequestState(object o, System.EventArgs e)
          {
              HttpApplication httpApp = (HttpApplication)o;
              HttpContext ctx = HttpContext.Current;
  
              string remotehost  = ctx.Request.ServerVariables["REMOTE_ADDR"];
              string httpuseragent = ctx.Request.ServerVariables["HTTP_USER_AGENT"];
              string requstmethod = ctx.Request.ServerVariables["REQUEST_METHOD"];
              string httpreferer = ctx.Request.ServerVariables["HTTP_REFERER"];
              string HTTPXFORWARDEDFOR = ctx.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
              string HTTPFORWARDEDFOR = ctx.Request.ServerVariables["HTTP_FORWARDED_FOR"];
              string HTTPXFORWARDED = ctx.Request.ServerVariables["HTTP_X_FORWARDED"];
  
  
              ctx.Response.Write("&lt;P&gt;REMOTE_ADDR: " + remotehost + "&lt;/P&gt;");
              ctx.Response.Write("&lt;P&gt;HTTP_USER_AGENT: " + httpuseragent + "&lt;/P&gt;");
              ctx.Response.Write("&lt;P&gt;REQUEST_METHOD: " + httpuseragent + "&lt;/P&gt;");
              ctx.Response.Write("&lt;P&gt;HTTP_REFERER: " + httpreferer + "&lt;/P&gt;");
              ctx.Response.Write("&lt;P&gt;HTTP_X_FORWARDED_FOR: " + httpreferer + "&lt;/P&gt;");
              ctx.Response.Write("&lt;P&gt;HTTP_FORWARDED_FOR: " + httpreferer + "&lt;/P&gt;");
              ctx.Response.Write("&lt;P&gt;HTTP_X_FORWARDED: " + httpreferer + "&lt;/P&gt;");
              ctx.Response.Close();
          }
      }
  }
  </code></pre>