http的GET和POST
本文主要内容
1、 GET和POST方法介绍
2、 源代码分析
3、 结果分析
4、 例子
参考及引用:
http://www.cnblogs.com/zhijianliutang/archive/2012/09/23/2698860.html
http://www.cnblogs.com/liuzhang/p/3929198.html
http://www.cnblogs.com/Daniel-G/p/3995854.html
-----------------------------------------------------------------------------------------------------------------
1、GET和POST方法介绍
html发送表单给服务器常用方法有两种:GET和POST。
<1> GET方法发送给服务器的数据保存在服务器的QUERY_STRING环境变量中。读取这个环境变量的值,就可以获得数据。发送的数据就直接接在html信息头的后面,作为数据的一部分,这个后面会说明。
<2> POST方法的数据是放在发送过去的信息体中。其数据的长度保存在服务器的CONTENT_LENGTH环境变量中,数据被重定向到了标准输入,只要从标准输入读取CONTENT_LENGTH长度的数据即可。读取之后提取其中有用的数据。
2、源代码
<1> html
如下,是我写的网页,action属性指定接收并处理这个表单的函数,method指定表单发送的方法。文件名:button.html
<html>
<form action="http://192.168.1.112/cgi-bin/button.cgi" method="get">
<input type="submit" name="button1" value="up"/>
<input type="submit" name="button2" value="down"/>
<input type="submit" name="button3" value="left"/>
<input type="submit" name="button4" value="right"/>
</form>
</html>
<2> 使用c语言写的cgi,文件名button.c。
#include <stdio.h>
#include <stdlib.h>
#include <string.h> char *getcgidata(FILE *fp, char *req_method)
{
char *input;
int length; /* GET */
if (strcmp(req_method, "GET") == ) {
/* get方法的数据放在QUERY_STRING中 */
input = getenv("QUERY_STRING");
/* 将结果打印到浏览器中 */
printf("<p>input:%s</p>", input);
return input;
/* POST */
} else if (strcmp(req_method, "POST") == ) {
length = atoi(getenv("CONTENT_LENGTH"));
/* CONTENT_LENGTH保存POST方法数据的长度 */
printf("length:%d\n", length);
input = malloc(sizeof(char)*length + );
/* 从标准输入读取数据 */
int i = ;
while (i < length) {
input[i] = getc(fp);
i++;
}
input[i] = '\0';
printf("<p>input : %s</p>", input);
return input;
}
return NULL;
} int main(int argc, char *argv[])
{
char *input;
char *req_method; /* 确定发给服务器数据的类型,需要有一个空行,
* 将文本内容空开*/
printf("Content-type:text/html\n\n");
/* 从环境变量获取发送过来数据的方法 */
req_method = getenv("REQUEST_METHOD");
printf("<p>req_method:%s</p>", req_method); input = getcgidata(stdin, req_method); free(input); return ;
}
我是在虚拟机中安装ubuntu,并配置apache服务器,在终端编译(注意生成的cgi文件的权限,其他具有可执行权限),并移动到apache服务器的cgi目录/var/www/cgi-bin。将button.html文件移动到/var/www。
gcc button.c –o button.cgi
sudo mv button.cgi /var/www/cgi-bin/
cp button.html /var/www/
3、结果分析
在windows上的浏览器搜索栏中输入“服务器的ip地址/buuton.html”,访问虚拟机中ubuntu上运行的apache服务器。例如我的服务器地址是192.168.1.112,在搜索栏中输入“192.168.1.112/button.html”。当然也可以直接在本地打开button.html文件,就不需要那么麻烦。如下图所示:
点击其中的某个按键,例如down。在搜索栏中可以看到,get的数据是在发送的信息头中,并且是在“?”之后。
使用Colasoft Capsa 7 Enterprise进行抓包,读取HTTP部分的信息。如下图所示,这就是发送给服务器的信息的信息头。
而将html中的method改正post之后,结果又会怎样呢?
点击down,结果如下:
可以看出,POST的数据是放在发送给服务器的数据信息体的内部的。抓包也可以看出。
4、例子
网页user.html
<html>
<form action="http://192.168.1.112/cgi-bin/user.cgi" method="POST">
<table>
<tr>
<td>name</td>
<td><input name="name" size="8" /></td>
</tr>
<tr>
<td>country</td>
<td><input name="country" size="8" /></td>
</tr>
<tr>
<td>sex</td>
<td><input name="sex" size="8" /></td>
</tr>
<tr>
<td>age</td>
<td><input name="age" size="8" /></td>
</tr>
<tr>
<td>submit</td>
<td><input type="submit" value="submit" size="8"></td>
</tr>
</table>
</form>
</html>
程序user.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h> typedef struct user_info {
char name[];
char country[];
char sex[];
int age;
} USER; char *get_data(FILE *fp, char *req_method);
int parse_data(char *input, USER *user); int main(int argc, char *argv[])
{
int length;
char *input;
char *req_method;
USER user; printf("content-type: text/html\n\n"); printf("<title>user information</title>");
printf("<h4>user info</h4>");
req_method = getenv("REQUEST_METHOD");
input = get_data(stdin, req_method);
/* 将获取的数据打印到浏览器中进行验证 */
printf("<p>data:%s</p>", input);
parse_data(input, &user); /* 解析后的数据打印到浏览器中 */
printf("<p>name:%s</p>", user.name);
printf("<p>country:%s</p>", user.country);
printf("<p>sex:%s</p>", user.sex);
printf("<p>age:%d</p>", user.age); free(input); return ;
}
/* 获取数据 */
char *get_data(FILE *fp, char *req_method)
{
char *input;
int length; if (strcmp(req_method, "GET") == ) {
input = getenv("QUERY_STRING");
return input;
} else if (strcmp(req_method, "POST") == ) {
length = atoi(getenv("CONTENT_LENGTH"));
printf("length:%d\n", length);
input = malloc(sizeof(char)*length + );
int i = ;
while (i < length) {
input[i] = getc(fp);
i++;
}
input[i] = '\0';
return input;
}
return NULL;
} /* 解析数据 */
int parse_data(char *input, USER *user)
{
char *str;
char *data; /* 分割数据的数据,strtok函数会将制定的分隔符"&"转化为'\0'
* 并返回分割出的字符串的首地址,str指向 "name=tony */
str = strtok(input, "&");
/* 指针偏移到'='后面,获取有用的数据 */
data = strchr(str, '=') + ;
/* 保存数据 */
sprintf(user->name,"%s",data); /* str指向country=China */
str = strtok(NULL, "&");
data = strchr(str, '=') + ;
sprintf(user->country,"%s",data); /* str指向sex=male */
str = strtok(NULL, "&");
data = strchr(str, '=') + ;
sprintf(user->sex,"%s",data); /* str指向age=23 */
str = strtok(NULL, "&");
data = strchr(str, '=') + ;
user->age = atoi(data); return ;
}
user.html放在/var/www/,cgi放到/var/www/cgi-bin
sudo cp user.html /var/www gcc user.c -o user.cgi sudo cp user.cgi /var/www/cgi-bin
浏览器输入:
点击submit,发送数据,抓包,可以知道post的数据信息,发送的数据保存在信息体中。
查看colasoft下面的数据帧,也是同样的结果。
浏览器输出结果
随机推荐
- Leetcode 二分查找 Search Insert Position
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 T ...
- openssl之EVP系列之9---EVP_Digest系列函数的一个样例
openssl之EVP系列之9---EVP_Digest系列函数的一个样例 ---依据openssl doc/crypto/EVP_DigestInit.pod翻译 (作者:Drago ...
- hive sql 修改列名
ALTER TABLE dev.dev_jypt_jiadian_cate3_pred_20181109 CHANGE utem_third_cate_name item_third_cate_nam ...
- Laravel 事件系统用法总结(监听事件,观察者模式)
看这篇文章先复习一下设计模式 : https://www.cnblogs.com/fps2tao/p/9640338.html 在理解了观察者模式后,我们开始正文 Laravel 的事件提供了一个简单 ...
- mysql 5.6.23的源码安装
http://634871.blog.51cto.com/624871/1325914 首先删除系统自带的mysql rpm -qa | grep mysql rpm -e mysql //普 ...
- C#用正则表达式一键Unicode转UTF8(解决LitJson中文问题)
txt = Regex.Unescape(txt);
- 2. 解题报告~买卖股票的最佳时机 II
原题地址:https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/1/array/22/ 给定一个数组, ...
- ping域名和ping IP时速度不同的原因
不知道大家在ping的时候有没有遇到过这样的问题:当你ping一个域名的时候,ping结果返回得很慢,但是如果直接ping这个域名的ip,结果却快很多. 直接ping ip的时候,每两次发包之间没有明 ...
- Vmware-虚拟机中ubuntu不能联网问题的解决——NAT方式
设置虚拟机不能联网是很痛苦的,这里我就ubuntu的NAT上网问题就个人经验讲一下,其他的桥连接等没有使用就没有经验了. 1.查看/设置下NAT的网络 打开VMware Workstation, 点击 ...
- Mysql 中创建索引和索引的使用问题
在数据库中合理的使用索引是提升mysql数据库的一种高效和快捷的方式,但是在索引的使用上在我的使用中发现有很多坑,因为自己之前没有认识到,所以来总结一下 索引的介绍 索引是一种特殊的文件,其中包含着对 ...