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.- Project must request and receive proxy ServerVariables to display on client side.
- Parse IPAddress and return Anonymity state.
- Implement Basic Athentifcation Scheme.
- Project must not use Scripts for functionality (e.g) PHP, Perl, Asp, etc.
- Multi-platform Compatible (possibilty)
Questions:
Is it possible to use Request.ServerVariables on a local Windows or Console Application or is it ASP specific?
If this method is ASP specific is there another way to request the ServerVariables from a browser session?
If the method above is possible what is the proper approach for achieving this functionality?
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/ProxyJudgeExample 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:
Request.ServerVariables can only be accessed on the web server
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.
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
- Not sure what you are talking about here, do you have some example code.
精彩评论