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\n\n");
    printf("Content-type: text/html\n\n");
    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] = '\0';
            break;
        }
        name[j++] = input[i];
    }

    for ( i = 19 + strlen(name), j = 0; i < (int)strlen(input); i++ )
    {
        if ( input[i] == '&' )
        {
            pass[j] = '\0';
            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] = '\0';
    printf("Your Username is %s<br>Your Password is %s<br>Your userid is %s<br> \n", 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] = '\0';
            return input;
        }
        while (1)
        {
            input[i] = (char)fgetc(fp);
            if (i == size)
            {
                input[i + 1] = '\0';
                return input;
            }
            --len;
            if (feof(fp) || (!(len)))
            {
                i++;
                input[i] = '\0';
                return input;
            }
            i++;
        }
    }
    return NULL;
}


【方法2】

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

[html]  view plain copy
  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 plain copy
  1. sscanf(info,"username=%[^&]&passwd=%[^&]&userid=%d",username,passwd,&userid);  
  2. free(info);  

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

 

【思考】如果子串为username=zhang; passwd=123; userid=012,如何用【方法2】提取?

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

注意:将%[^&]&替换为%[^;];。通过本例,可以明白%[^;];的用法。

【解析】%[^&]是正则表达式,详细请参考:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要写一个Java微信客服接口,需要使用微信提供的开发者文档和SDK。在文档,有详细的说明如何通过读取消息接口主动读取具体的消息内容。 具体来说,可以使用Java的HTTP请求库(如Apache HttpClient或OkHttp)编写POST请求接口,并在请求体传递需要读取消息内容。在请求头需要设置必要的参数,如接口访问令牌等。 以下是示例代码: ```java import okhttp3.*; import java.io.IOException; public class WeChatMessageReader { public static void main(String[] args) throws IOException { OkHttpClient client = new OkHttpClient().newBuilder().build(); MediaType mediaType = MediaType.parse("application/json"); RequestBody body = RequestBody.create(mediaType, "{\n \"msg_id\": \"1234567890\",\n \"offset\": 0,\n \"count\": 10\n}"); Request request = new Request.Builder() .url("https://api.weixin.qq.com/cgi-bin/message/mass/get?access_token=ACCESS_TOKEN") .method("POST", body) .addHeader("Content-Type", "application/json") .build(); Response response = client.newCall(request).execute(); System.out.println(response.body().string()); } } ``` 在上面的示例代码,我们使用了OkHttp库发送一个POST请求,请求的URL为微信提供的读取消息接口,请求体包含了需要读取消息ID、偏移量和数量。请求头包含了Content-Type和Access-Token两个必要的参数。 注意,示例代码的Access-Token需要被替换为真实的接口访问令牌,这个令牌可以在微信开放平台申请获得。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值