• cgi程序读取post发送的特殊字符,尤其适合于微信公众平台开发中发送被动消息


    【问题】用c编写cgi程序怎样取出html表单post来的数据?

    【分析】html表单post来的数据形如username="zhang"&&password="123456"&&useid="012"

    【方法1】lainco


    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    char *getcgidata(FILE *fp, char *requestmethod);
    int main()
    {
        char *input;
        char *req_method;
        char name[64];
        char pass[64];
        char userid[64];
        int i = 0;
        int j = 0;
        // printf("Content-type: text/plain; charset=iso-8859-1
    
    ");
        printf("Content-type: text/html
    
    ");
        printf("The following is query reuslt:<br><br>");
        req_method = getenv("REQUEST_METHOD");
        input = getcgidata(stdin, req_method);
    
        for ( i = 9; i < (int)strlen(input); i++ )
        {
            if ( input[i] == '&' )
            {
                name[j] = '';
                break;
            }
            name[j++] = input[i];
        }
    
        for ( i = 19 + strlen(name), j = 0; i < (int)strlen(input); i++ )
        {
            if ( input[i] == '&' )
            {
                pass[j] = '';
                break;
            }
            pass[j++] = input[i];
        }
    
        for ( i = 30 + strlen(pass) + strlen(name), j = 0; i < (int)strlen(input); i++ )
        {
            userid[j++] = input[i];
        }
    
        userid[j] = '';
        printf("Your Username is %s<br>Your Password is %s<br>Your userid is %s<br> 
    ", name, pass, userid);
        return 0;
    }
    char *getcgidata(FILE *fp, char *requestmethod)
    {
        char *input;
        int len;
        int size = 1024;
        int i = 0;
        if (!strcmp(requestmethod, "GET"))
        {
            input = getenv("QUERY_STRING");
            return input;
        }
        else if (!strcmp(requestmethod, "POST"))
        {
            len = atoi(getenv("CONTENT_LENGTH"));
            input = (char *)malloc(sizeof(char) * (size + 1));
            if (len == 0)
            {
                input[0] = '';
                return input;
            }
            while (1)
            {
                input[i] = (char)fgetc(fp);
                if (i == size)
                {
                    input[i + 1] = '';
                    return input;
                }
                --len;
                if (feof(fp) || (!(len)))
                {
                    i++;
                    input[i] = '';
                    return input;
                }
                i++;
            }
        }
        return NULL;
    }


    【方法2】

    1 先将post来的数据总体读入info

    [html] view plaincopy
    1. char *info=NULL;  
    2.    
    3.  char username[64];  
    4.  char passwd[64];  
    5.  int userid;  
    6.   
    7.    
    8.  int lenstr=0;  
    9.   
    10.  memset(username,0,64);  
    11.  memset(passwd,0,64);  
    12.  userid=0;   
    13. /*  
    14. * Get the data by post method from index.html  
    15. */  
    16.  lenstr=atoi(getenv("CONTENT_LENGTH"));  
    17.  info=(char *)malloc(lenstr+1);  
    18.  fread(info,1,lenstr,stdin);  

     2 将info作为文件流输入。利用sscanf提取子串

    [html] view plaincopy
    1. sscanf(info,"username=%[^&]&passwd=%[^&]&userid=%d",username,passwd,&userid);  
    2. free(info);  

     注:假设数据为实型。则利用sscanf时。要加上&,如上例userid

     

    【思考】假设子串为username=zhang; passwd=123; userid=012,怎样用【方法2】提取?

    [html] view plaincopy
    1. sscanf(info,"username=%[^;]; passwd=%[^;]; userid=%d",&username,&passwd,&userid);  

    注意:将%[^&]&替换为%[^;];。

    通过本例,能够明确%[^;];的使用方法。

    【解析】%[^&]是正則表達式,具体请參考:

    http://blog.chinaunix.net/space.php?uid=9195812&do=blog&cuid=499274

  • 相关阅读:
    Web.config配置详解
    vs2010下创建webservice
    WinForm如何调用Web Service
    Android动画的两种使用方式。
    ES6介绍
    django批量form表单处理
    django生命周期示意图
    django中的构造字典(二级菜单,评论树,购物车)
    django中介模型,CBV模型,及logging日志配制
    django中csrftoken跨站请求伪造的几种方式
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/7137845.html
Copyright © 2020-2023  润新知