【问题标题】:Passing CGI arguments to an executable in Apache on Windows将 CGI 参数传递给 Windows 上 Apache 中的可执行文件
【发布时间】:2009-09-03 15:15:56
【问题描述】:

我的印象是我可以将任何旧的可执行程序放在 Apache 的cgi-bin 目录中,并将其用作 CGI 脚本。具体来说,如果我有一个 C# 程序

static class TestProg
{
    static void Main(string[] args)
    {
        Console.Write("Content-type: text/plain\r\n\r\n");
        Console.WriteLine("Arguments:");
        foreach (string arg in args)
            Console.WriteLine(arg);
    }
}

然后转到http://example.com/cgi-bin/TestProg?hello=kitty&goodbye=world 然后查询字符串hello=kitty&goodbye=world 将作为第一个参数传递给main,所以我的页面应该是这样的

Arguments:
hello=kitty&goodbye=world

不幸的是,我的查询参数都没有被传递;页面加载并仅打印Arguments:,后面没有任何内容。

那么如何让我的查询参数传递给这个程序呢?

【问题讨论】:

    标签: apache executable cgi-bin query-parameters


    【解决方案1】:

    参数不会在命令行上传递 - 相反,apache 在调用 cgi 程序之前设置环境变量 (http://httpd.apache.org/docs/2.0/howto/cgi.html#behindscenes)。

    您可以访问包含查询字符串文本的环境变量“QUERY_STRING”。

     String queryString = System.Environment.GetEnvironmentVariable("QUERY_STRING");
    

    然后您需要自己解析 queryString。

    但是,POST 数据是通过 STDIN 传递的,因此您需要使用 Console.In 来处理它。

    【讨论】:

      【解决方案2】:

      自从我使用 CGI 和 Apache 以来已经有很长时间了,但如果我没记错的话,查询字符串是 Apache 中的一个环境变量。在 C# 中,您可以使用 System.Environment.GetEnvironmentVariables 查看环境。我没有任何已发布的文档来支持我,但我会先试试看。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-05-05
        • 1970-01-01
        • 1970-01-01
        • 2012-05-05
        • 1970-01-01
        • 1970-01-01
        • 2014-06-19
        • 2014-07-02
        相关资源
        最近更新 更多