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下面的数据帧,也是同样的结果。
浏览器输出结果
随机推荐
- C# GridView 给某行或某列绑定点击事件和鼠标事件
protected void GridViewEx1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType = ...
- Apache优化:修改最大并发连接数(转)
Apache是一个跨平台的web服务器,由于其简单高效.稳定安全的特性,被广泛应用于计算机技术的各个领域.现在,Apache凭借其庞大的用户数,已成为用户数排名第一的web服务器. 尽 管如此,在实际 ...
- cocos2dx 3.x ccPositionTextureColor_vert与ccPositionTextureColor_noMVP_vert
在cocos2dx 2.x中,如果我们要对sprite更换片段shader,写成: myProgram->initWithByteArrays(ccPositionTextureColor_ve ...
- FPGA基础设计(四):IIC协议
很多数字传感器.数字控制的芯片(DDS.串行ADC.串行DAC)都是通过IIC总线来和控制器通信的.不过IIC协议仍然是一种慢速的通信方式,标准IIC速率为100kbit/s,快速模式速率为400kb ...
- 【Android】7.3 GridLayout(网格布局)
分类:C#.Android.VS2015: 创建日期:2016-02-10 一.简介 Android 4.0(API 14)开始提供的GridLayout布局使用虚细线将布局划分为行.列和单元格,也支 ...
- gulp的使用 文档
#gulp ##1 什么是gulp.为什么使用gulp Gulp是一个**构建系统**,它能通过自动执行常见任务,比如编译预处理CSS,压缩JavaScript,来改进网站开发的过程. 将less文件 ...
- MVC,MVP 和 MVVM 的图示 转自阮一峰先生网络日志
MVC,MVP 和 MVVM 的图示 作者: 阮一峰 复杂的软件必须有清晰合理的架构,否则无法开发和维护. MVC(Model-View-Controller)是最常见的软件架构之一,业界有着广泛 ...
- js 学习的地址;
1.https://github.com/windiest/Front-end-tutorial 2.http://www.w3school.com.cn/tags/html_ref_eve ...
- 在开发JavaBean的过程中打开Tomcat的reloadable
这样可以方便调试,就不用每次修改JavaBean都要重启服务器了,但是要记得,项目deploy阶段的时候要关闭这个选项(考虑服务器的性能问题) 配置Tomcat的/conf/server.xml即可 ...
- 续写上一篇的数组or指针操作
C语言,同样使用if else while 这样的语法,但不同的人,就是有不同的实现方式,甚至是技巧. eg: #include <stdio.h> #include<string. ...