gpt4 book ai didi

C# 在 Windows 窗体或控制台上使用 HttpListener 和 Request.ServerVariables

转载 作者:可可西里 更新时间:2023-11-01 14:28:53 28 4
gpt4 key购买 nike

项目目标:

使用控制台或 Windows 窗体应用程序创建本地 Proxy Judge 以调试和测试连接。

  1. 项目必须请求并接收代理 ServerVariables 以在客户端显示。
  2. 解析 IP 地址并返回匿名状态。
  3. 实现基本认证计划。
  4. 项目不得使用脚本实现功能(例如 PHP、Perl、Asp 等)。
  5. 多平台兼容(可能)

alt text


问题:

  1. 是否可以在本地 Windows 或控制台应用程序上使用 Request.ServerVariables 还是特定于 ASP?

  2. 如果此方法是特定于 ASP 的,是否还有其他方法可以从浏览器 session 中请求 ServerVariables?

  3. 如果上述方法可行,实现此功能的正确方法是什么?

  4. 这里有什么验证/设置基本身份验证方案的好例子?比如设置要使用的密码和用户等。


使用的引用资料:

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

最佳答案

您的代码正在合并 ASP.NET 逻辑和应用程序逻辑,这是不能/不应该完成的。

A IHttpModule由 IIS 在 ASP.NET WEB 应用程序中运行

A Main方法由控制台应用程序运行

问题:

  1. Request.ServerVariables只能在web服务器上访问

  2. 您使用的方法 (app_PostAcquireRequestState) 将变量输出到响应流,我就是这样做的。但通常不在 HttpModule 或特定方法中。尝试将变量输出到 ASP.NET 管道的末尾。

  3. 您可以在 web.config 中打开跟踪 <trace> ,这应该会输出您需要的一些变量。

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

  1. 不确定您在这里谈论的是什么,您是否有一些示例代码。

关于C# 在 Windows 窗体或控制台上使用 HttpListener 和 Request.ServerVariables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4731179/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com