在 Windows 窗体或控制台上使用 HttpListener 和 Request.ServerVariables 的 C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4731179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# using HttpListener and Request.ServerVariables on Windows Forms or Console
提问by Nightforce2
Project Objectives:
Create a local Proxy Judge using a Console or Windows Form application for debugging and testing connections.
项目目标:
使用控制台或 Windows 窗体应用程序创建本地代理法官,用于调试和测试连接。
- 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)
- 项目必须请求和接收代理 ServerVariables 才能在客户端显示。
- 解析 IPAddress 并返回匿名状态。
- 实施基本认证计划。
- 项目不得将脚本用于功能(例如)PHP、Perl、Asp 等。
- 多平台兼容(可能性)
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.
是否可以在本地 Windows 或控制台应用程序上使用 Request.ServerVariables 或者它是特定于 ASP 的?
如果此方法是特定于 ASP 的,是否还有另一种方法可以从浏览器会话中请求 ServerVariables?
如果上述方法可行,实现此功能的正确方法是什么?
在这里验证/设置基本身份验证方案的好例子是什么?比如设置要使用的密码和用户等等。
References used:
http://msdn.microsoft.com/en-us/library/system.web.httpapplication.aspxhttp://www.java2s.com/Code/CSharpAPI/System.Net/HttpListenerContextResponseStatusCode.htmhttp://en.cship.org/wiki/ProxyJudge
使用的参考资料: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();
}
}
}
回答by djeeg
Your code is merging ASP.NET logic and Application logic, which can't/shouldn't be done.
您的代码正在合并 ASP.NET 逻辑和应用程序逻辑,这不能/不应该完成。
A IHttpModule
is run by IIS in a ASP.NET WEB application
AIHttpModule
由 IIS 在 ASP.NET WEB 应用程序中运行
A Main
method is run by a console application
甲Main
方法由控制台应用程序运行
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.
Request.ServerVariables 只能在 web 服务器上访问
您将变量输出到响应流的方法 (app_PostAcquireRequestState) 就是我的方法。但通常不在 HttpModule 或该特定方法中。尝试在 ASP.NET 管道的末尾输出变量。
您可以在 web.config 中打开跟踪
<trace>
,这应该会输出您需要的一些变量。
http://msdn.microsoft.com/en-us/library/6915t83k.aspx
http://msdn.microsoft.com/en-us/library/6915t83k.aspx
- Not sure what you are talking about here, do you have some example code.
- 不知道你在说什么,你有一些示例代码。