加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 21:14:28 所属栏目:百科 来源:网络整理
导读:项目目标: 使用控制台或 Windows窗体应用程序创建本地代理判断程序,以调试和测试连接. 项目必须请求并接收代理ServerVariables以在客户端显示. 解析IPAddress并返回匿名状态. 实施基本的教学方案. 项目不得使用脚本来实现功能(例如)PHP,Perl,Asp等. 多平台
项目目标:
使用控制台或 Windows窗体应用程序创建本地代理判断程序,以调试和测试连接.

>项目必须请求并接收代理ServerVariables以在客户端显示.
>解析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();
        }
    }
}

解决方法

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

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

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

问题:

> Request.ServerVariables只能在Web服务器上访问
>您将方法输出到响应流的方法(app_PostAcquireRequestState)是我如何做到的.但通常不在HttpModule或特定方法中.尝试将变量输出到ASP.NET管道的末尾.
>您可以在web.config< trace>中打开跟踪,该输出应输出您需要的一些变量.

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

>不确定你在这里谈论的是什么,你有一些示例代码吗?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读