博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Apache下c语言的cgi如何获得Get,Post参数
阅读量:4069 次
发布时间:2019-05-25

本文共 1284 字,大约阅读时间需要 4 分钟。

Apache下的c语音的cgi如何获得Get,Post参数

 

作为实验,自己在ubuntu上搭建apache服务器,提供了网络服务(这一步比较简单,后续补充上来)。遇到的一个问题是,如何获得页面提交的Get或者Post函数。下面会给出具体的源码来实现这一个目的。

 

先看整体效果图

这样来提交到apache上的test.cgi,应该可以正常显示”hello world”

 

1.      先看该html的页面的源码,标红色的为两个button

 

<html>                                                                         

    <head>

        <title>This isMichael</title>

    </head>

    <body>

        <formaction="/cgi-bin/test.cgi" method="get">

               <input type="text" name="text" value='helloworld'/>

               <input type="submit" value="Get" method=get />

        </form>

 

        <formaction="/cgi-bin/test.cgi" method="post">

               <input type="text" name="text" value="helloworld"/>

               <input type="submit" value="Post" method=post/>

        </form>

   </body>

</html>

2.      C编写的test.cgi 如下,标红色的地方为核心,表示如何调用系统函数获得对应的环境变量:

include<stdio.h>

#include<stdlib.h>

#include<unistd.h>

#include<time.h>

#include<iostream>

 

usingnamespace std;

 

 

intmain()

{

        printf("Content-type:text/html\n\n");

 

        /* Parameter by Get Method */

        char *param =getenv("QUERY_STRING");

        while( param != NULL && *param!= '\0' )

        {

                printf("%c", *param);

                param++;

        }                                                                                                                             

        /* Parameter by Post Method */

        int n=0;

       if(getenv("CONTENT_LENGTH") )

               n=atoi(getenv("CONTENT_LENGTH") );

        printf("thecontent length is %d", n );

        for (int i=0;i<n;i++)

                putchar(getchar());

        putchar('\n');

        fflush(stdout);

 

        return 0;

}

                                                         

 

经过我的测试,上面的代码是可以运行的。但是有个小细节要注意一下

“hello world” 在作为参数传递时会变成 “hello+world”.

额,这个有点恶心,如果有哪位大牛知道为什么,欢迎回帖说明原因。

转载地址:http://vpoji.baihongyu.com/

你可能感兴趣的文章
maven struts2中,使用ModelDriven
查看>>
通过web.xml传递初始参数
查看>>
过滤器与拦截器
查看>>
maven 下成功搭建hibernate
查看>>
maven hibernate 实现对mysql简单的CRUD操作
查看>>
Java 线程池学习
查看>>
j2ee中的listener、 filter、servlet 加载顺序及其详解
查看>>
j2ee中web.xml中配置listener
查看>>
web.xml中load-on-startup的作用
查看>>
MySQL导入sql 文件的5大步骤
查看>>
释放MySQL ibdata1文件的空间
查看>>
is not in the sudoers file. This incident will be reported.
查看>>
win7 Centos 双系统的时间问题
查看>>
CentOs安装无线网卡后,一段时间后不用,网络自动断开的解决办法
查看>>
Centos 成功安装NTFS-3G
查看>>
Linux下tar.xz结尾的文件的解压方法
查看>>
yum install vlc
查看>>
Java技术路线图
查看>>
Java学习的30个目标
查看>>
Spring2.5+MINA2搭建Socket Server
查看>>