简体   繁体   中英

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

Project Objectives:

Create a local Proxy Judge using a Console or Windows Form application for debugging and testing connections.

  1. Project must request and receive proxy ServerVariables to display on client side.
  2. Parse IPAddress and return Anonymity state.
  3. Implement Basic Athentifcation Scheme.
  4. Project must not use Scripts for functionality (eg) PHP, Perl, Asp, etc.
  5. Multi-platform Compatible (possibilty)

替代文字


Questions:

  1. Is it possible to use Request.ServerVariables on a local Windows or Console Application or is it ASP specific?

  2. If this method is ASP specific is there another way to request the ServerVariables from a browser session?

  3. If the method above is possible what is the proper approach for achieving this functionality?

  4. 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.


References used:

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

Example 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("<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();
        }
    }
}

Your code is merging ASP.NET logic and Application logic, which can't/shouldn't be done.

A IHttpModule is run by IIS in a ASP.NET WEB application

A Main method is run by a console application

Questions:

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

  2. The method that you have (app_PostAcquireRequestState) where you are outputing the variables to the response stream, is how i do it. But not normally in a HttpModule or in that particular method. Try to output the variables towards the end of the ASP.NET pipeline.

  3. You could turn on the tracing in web.config <trace> , that should output some of the variables you require.

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

  1. Not sure what you are talking about here, do you have some example code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM