zoukankan      html  css  js  c++  java
  • CGI原理解析系列之中的一个----CGI怎样获取WEBserver数据


    //gcc get_post.c -o get_post.ums;


    #include <stdio.h>

    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>

    int main(int argc,char *argv[])
    {
        size_t i = 0,n = 0;
        printf("Content-Type:text/plain ");
        char * method = NULL;
        //获取HTTP请求方法(POST/GET)
        if(NULL == (method = getenv("REQUEST_METHOD")))
        {
            return 0;
        }

        
        if(getenv("CONTENT_LENGTH") && strcmp(method,"POST") == 0)
        {
            //POST 方法解析,从 STDIN_FILENO 动态获取数据
            n = atoi(getenv("CONTENT_LENGTH"));
            size_t length = n * sizeof(char) + 1;
            char * inputdata = (char *)malloc(length);
            if(inputdata)
            {
                memset((void*)inputdata,0,length);
                if(n != read(STDIN_FILENO,inputdata,n));
                {
                    //
                }
                printf("hello %s,cgi post call. ",inputdata);
                free(inputdata);
            }
        }
        else if(getenv("QUERY_STRING") && strcmp(method,"GET") == 0)
        {
            //环境变量的长度有限,导致GET方法传送数据被限制
            char * inputdata = getenv("QUERY_STRING");
            printf("hello %s,get call. ",inputdata);
        }
        else
        {
            printf("%s ","bad request!");
        }
        fprintf(stdout,"finish,data length %d ",n);
        return 0;    

    }




  • 相关阅读:
    Google 地图小例子
    实现网站的中英文转换
    必须掌握的八个【cmd 命令行】
    C#发送email
    sql 参数化查询,添加,删除
    SqlDataReader使用序数索引器
    DataSet数据筛选和排序
    Connect Propertity
    SqlDataReader读取数据
    SqlDataReader读取数据
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4037016.html
Copyright © 2011-2022 走看看