码迷,mamicode.com
首页 > 微信 > 详细

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

时间:2014-10-23 14:30:21      阅读:377      评论:0      收藏:0      [点我收藏+]

标签:cgi   post data   微信公众平台开发   

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

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

【方法1】

[html] view plaincopy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>   
  4. char* getcgidata(FILE* fp, char* requestmethod);  
  5. int main()  
  6. {  
  7. char *input;  
  8. char *req_method;  
  9. char name[64];  
  10. char pass[64];  
  11. char userid[64];  
  12. int i = 0;  
  13. int j = 0;  
  14. // printf("Content-type: text/plain; charset=iso-8859-1\n\n");  
  15. printf("Content-type: text/html\n\n");  
  16. printf("The following is query reuslt:<br><br>");  
  17. req_method = getenv("REQUEST_METHOD");  
  18. input = getcgidata(stdin, req_method);  
  19.   
  20. for ( i = 9; i < (int)strlen(input); i++ )  
  21. {  
  22. if ( input[i] == ‘&‘ )  
  23. {  
  24. name[j] = ‘\0‘;  
  25. break;  
  26. }   
  27. name[j++] = input[i];  
  28. }  
  29.   
  30. for ( i = 19 + strlen(name), j = 0; i < (int)strlen(input); i++ )  
  31. {  
  32. if ( input[i] == ‘&‘ )  
  33. {  
  34. pass[j] = ‘\0‘;  
  35. break;  
  36. }   
  37. pass[j++] = input[i];  
  38. }  
  39.   
  40. for ( i = 30 + strlen(pass)+ strlen(name), j = 0; i < (int)strlen(input); i++ )  
  41. {  
  42. userid[j++] = input[i];  
  43. }  
  44.   
  45. userid[j] = ‘\0‘;  
  46. printf("Your Username is %s<br>Your Password is %s<br>Your userid is %s<br> \n", name, pass,userid);  
  47. return 0;  
  48. }  
  49. char* getcgidata(FILE* fp, char* requestmethod)  
  50. {  
  51. char* input;  
  52. int len;  
  53. int size = 1024;  
  54. int i = 0;  
  55. if (!strcmp(requestmethod, "GET"))  
  56. {  
  57. input = getenv("QUERY_STRING");  
  58. return input;  
  59. }  
  60. else if (!strcmp(requestmethod, "POST"))  
  61. {  
  62. len = atoi(getenv("CONTENT_LENGTH"));  
  63. input = (char*)malloc(sizeof(char)*(size + 1));  
  64. if (len == 0)  
  65. {  
  66. input[0] = ‘\0‘;  
  67. return input;  
  68. }  
  69. while(1)  
  70. {  
  71. input[i] = (char)fgetc(fp);  
  72. if (i == size)  
  73. {  
  74. input[i+1] = ‘\0‘;  
  75. return input;  
  76. }  
  77. --len;  
  78. if (feof(fp) || (!(len)))  
  79. {  
  80. i++;  
  81. input[i] = ‘\0‘;  
  82. return input;  
  83. }  
  84. i++;  
  85. }  
  86. }  
  87. return NULL;  
  88. }  


【方法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

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

标签:cgi   post data   微信公众平台开发   

原文地址:http://blog.csdn.net/nyist327/article/details/40395779

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!