4

プロジェクトの目的:

コンソールまたは Windows フォーム アプリケーションを使用してローカルの Proxy Judge を作成し、接続のデバッグとテストを行います。

  1. プロジェクトは、クライアント側で表示するプロキシ サーバー変数を要求して受け取る必要があります。
  2. IPAddress を解析し、匿名状態を返します。
  3. 基本的な認証スキームを実装します。
  4. プロジェクトでは、機能 (PHP、Perl、Asp など) にスクリプトを使用してはなりません。
  5. マルチプラットフォーム対応(可能)

代替テキスト


質問:

  1. ローカルの Windows またはコンソール アプリケーションで Request.ServerVariables を使用することは可能ですか、それとも ASP 固有ですか?

  2. このメソッドが ASP 固有の場合、ブラウザ セッションから 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();
        }
    }
}
4

1 に答える 1

1

あなたのコードは、ASP.NET ロジックとアプリケーション ロジックをマージしていますが、これは実行できない/すべきではありません。

AIHttpModuleは、ASP.NET WEB アプリケーションで IIS によって実行されます。

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. ここで何について話しているのかわからない、サンプルコードはありますか。
于 2011-01-19T03:24:20.460 に答える