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下面的数据帧,也是同样的结果。
浏览器输出结果
随机推荐
- Linux内核中锁机制之完成量、互斥量
在上一篇博文中笔者分析了关于信号量.读写信号量的使用及源码实现,接下来本篇博文将讨论有关完成量和互斥量的使用和一些经典问题. 八.完成量 下面讨论完成量的内容,首先需明确完成量表示为一个执行单元需要等 ...
- win7 64 python2 xgboost安装
综述: 首先,关于xgboost是啥,可以看这一篇:机器学习(四)--- 从gbdt到xgboost 安装Python3 环境下的xgboost 可以通过pip install , 在网址中下载对应版 ...
- MongoDB索引类型
与关系型数据库一样,合理的使用索引可以大幅提高MongoDB的查询效率,本文介绍基础索引.复合索引.文档索引等几种常用索引的使用. 1. 基础索引与复合索引 1.1 基础索引 创建索引时,可以是一个集 ...
- centos7 SSH链接不上
我试了下面的方法不行--(并且也排查了 ssh是正确安装的) [一]关闭selinuxvi /etc/selinux/config 然后reboot重启!!!! [二]关闭防火墙并禁止启动,有能力自己 ...
- osX显示隐藏文件
终端输入: defaults write com.apple.finder AppleShowAllFiles -bool YES
- 基于PCIe的高速接口设计
基于PCIe的高速接口设计 由 judyzhong 于 星期四, 03/03/2016 - 13:49 发表 作者:李晓宁,姚远程,秦明伟 2016年微型机与应用第1期 摘要:PCIe总线是第三代I/ ...
- [na]出口选路pbr小实验视频
什么是策略路由? 一般都是部署在出口路由器,用于路径强制分发的, 优先级高于路由表. 策略路由小实验视频 这个是读书时候录的一个策略路由小实验
- 简单了解一下c编译过程
大一的时候,学习c语言,用的是VC6.0.用了1年多,到后来了解了Linux,知道了gcc编译器,开始使用gcc Hello.c -o a.out 这样的命令进行编译.后来又学了gcc的一些其他的命令 ...
- ios笔记一(面向对象编程)
#import <Foundation/Foundation.h> #import "Person.h" int main (int argc, const char ...
- SQL SEREVR IO
Designing High Performance I/O Systems for SQL Server https://sqlbits.com/Downloads/86/Designing%20I ...