C # using HttpListener and Request.ServerVariables in Windows Forms or Console

Project Goals:

Create a local proxy suffix using the console or a Windows Form application to debug and test connections.

  • The project must request and receive ServerVariables proxies for display on the client side.
  • Parsing IPAddress and returning anonymity state.
  • Implement the basic Athentifcation framework.
  • A project should not use scripts for functionality (e.g. PHP, Perl, Asp, etc.
  • Multi-platform compatibility (possible)

alt text


Questions:

  • Can I use Request.ServerVariables in a local Windows application or Console, or specifically ASP?

  • If this method is ASP specific, is there another way to request ServerVariables from a browser session?

  • If the above method is possible, then what approach is suitable to achieve this functionality?

  • What is a good example for checking / setting a basic authentication scheme here? How to set a password and user to use, etc.


Used links:

http://msdn.microsoft.com/en-us/library/system.web.httpapplication.aspx http://www.java2s.com/Code/CSharpAPI/System.Net/HttpListenerContextResponseStatusCode.htm http: // en. cship.org/wiki/ProxyJudge

Code example:

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("<P>Hello, {0}</P>", name); writer.WriteLine("<ul>"); foreach (string header in ctx.Request.Headers.Keys) { writer.WriteLine("<li><b>{0}:</b> {1}</li>", header, ctx.Request.Headers[header]); } writer.WriteLine("</ul>"); 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("<P>REMOTE_ADDR: " + remotehost + "</P>"); ctx.Response.Write("<P>HTTP_USER_AGENT: " + httpuseragent + "</P>"); ctx.Response.Write("<P>REQUEST_METHOD: " + httpuseragent + "</P>"); ctx.Response.Write("<P>HTTP_REFERER: " + httpreferer + "</P>"); ctx.Response.Write("<P>HTTP_X_FORWARDED_FOR: " + httpreferer + "</P>"); ctx.Response.Write("<P>HTTP_FORWARDED_FOR: " + httpreferer + "</P>"); ctx.Response.Write("<P>HTTP_X_FORWARDED: " + httpreferer + "</P>"); ctx.Response.Close(); } } } 
+4
source share
1 answer

Your code combines ASP.NET logic and application logic, which cannot / should not be executed.

A IHttpModule launched by IIS in an ASP.NET WEB Application

A Main Method Launched by a Console Application

Questions:

  • Request.ServerVariables can only be accessed on the web server

  • The method you have (app_PostAcquireRequestState) where you output the variables to the response stream is how I do it. But usually not in the HttpModule or in this particular method. Try putting variables at the end of the ASP.NET pipeline.

  • You can enable tracing in web.config <trace> , which should output some required variables.

http://msdn.microsoft.com/en-us/library/6915t83k.aspx

  • Not sure what you are saying here, you have some sample code.
+1
source

Source: https://habr.com/ru/post/1336076/


All Articles