项目目标:
使用控制台或 Windows 窗体应用程序创建本地代理裁判,以调试和测试连接。
- 项目必须请求和接收代理服务器变量才能在客户端显示。
- 解析 IPAddress 并返回匿名状态。
- 实施基本认证方案。
- 项目不得将脚本用于功能(例如)PHP、Perl、Asp 等。
- 多平台兼容(可能)
问题:
是否可以在本地 Windows 或控制台应用程序上使用 Request.ServerVariables 还是特定于 ASP?
如果此方法是特定于 ASP 的,是否有另一种方法可以从浏览器会话中请求 ServerVariables?
如果上述方法可行,那么实现此功能的正确方法是什么?
在这里验证/设置基本身份验证方案的一个很好的例子是什么?比如设置要使用的密码和用户等等。
使用的参考: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
示例代码:
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();
}
}
}