动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
  
   Cgi/Perl → 阅读文章

 深入研究表单提交方式:GET/POST

作者来源: 
阅读 3164 人次 , 2006-3-29 4:03:00 


本文平台:windows 2000 professional + apache 1.3.17 + perl 5.6.1 + internet explorer 5.00.2920.0000

  大家知道目前表单提交的方式有get和post。我在这里不多说什么,给大家看一个以get方式提交的表单的请求:

get /cgi-bin/tech/method.cgi?get=get http/1.1
accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*
referer: http://localhost//other.html
accept-language: zh-cn
accept-encoding: gzip, deflate
user-agent: mozilla/4.0 (compatible; msie 5.01; windows nt 5.0)
host: localhost:8080
connection: keep-alive

  这个请求是我们通过这个html代码发出的:

<form action="http://localhost:8080/cgi-bin/tech/method.cgi" method="get">
<input type="text" size="10" value="get" name="get">
<input type=submit value="get方式">
</form>

  这个请求已经超出了我们研究的范围,我们只研究其中的第一行。其中,第一个"get"说出了提交的方式,是以get方式提交的;中间的就是提交给服务器上哪个程序,前面一部分"/cgi-bin/tech/method.cgi"就是我们html的form中action的内容,而后面的"get=get"就是html的form中,input的内容:我们发现ie已经把这个表单的内容转换成特定格式了。在perl中,通过$get=$env{'query_string'}获得以get发送的数据。

  我们再看一个以post方式提交的表单的请求:

post /cgi-bin/tech/method.cgi http/1.1
accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-
powerpoint, application/vnd.ms-excel, application/msword, */*
referer: http://localhost//other.html
accept-language: zh-cn
content-type: application/x-www-form-urlencoded
accept-encoding: gzip, deflate
user-agent: mozilla/4.0 (compatible; msie 5.01; windows nt 5.0)
host: localhost:8080
content-length: 9
connection: keep-alive

post=post

  同样给出html:

<form action="http://localhost:8080/cgi-bin/tech/method.cgi" method="post">
<input type="text" size="10" value="post" name="post">
<input type=submit value="post方式">
</form>

  我们发现其中的数据跑到了最下面。在perl中,通过read(stdin,$post,$env{'content_length'})获得以post发送的数据。我记得get发送数据最多只能1024字节,而post好像很大很大!

  思考:如果我有这么一段html代码,它将会出现什么问题呢?

<form action="http://localhost:8080/cgi-bin/tech/method.cgi?get=get" method="post">
<input type="text" size="10" value="post" name="post">
<input type=submit value="get/post方式">
</form>

  这个代码在很多程序上可能用到过,但是大多数人不会好好的想一想,究竟哪些内容是以get发送的,哪些内容是以post发送的。我们看看它的请求是什么:

post /cgi-bin/tech/method.cgi?get=get http/1.1
accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-
powerpoint, application/vnd.ms-excel, application/msword, */*
referer: http://localhost//other.html
accept-language: zh-cn
content-type: application/x-www-form-urlencoded
accept-encoding: gzip, deflate
user-agent: mozilla/4.0 (compatible; msie 5.01; windows nt 5.0)
host: localhost:8080
content-length: 9
connection: keep-alive

post=post

  哈!原来是以post发送的。但是,你一定发现了有一部分数据放在了第一行,就是和get的情况一样的。其实这个例子很典型,是post和get混发!
   不相信你在perl中,用read(stdin,$post,$env{'content_length'})和$get=$env{'query_string'}看看,到底哪个里面有"get=get"这个数据。

  我给大家提供设备,大家自己去研究研究:

html部分:  

<html>
<head>
<title>get-post</title>
</head>

<body>
<form action="/cgi-bin/tech/method.cgi" method="get">
<input type="text" size="10" value="get" name="get">
<input type=submit value="get方式">
</form>
<form action="/cgi-bin/tech/method.cgi" method="post">
<input type="text" size="10" value="post" name="post">
<input type=submit value="post方式">
</form>
<form action="/cgi-bin/tech/method.cgi?get=get" method="post">
<input type="text" size="10" value="post" name="post">
<input type=submit value="get/post方式">
</form>
<form action="/cgi-bin/tech/method.cgi?name=hackfan&age=16&email=hackfan@163.net" method="post">
<input type="text" size="10" value="suzhou" name="address">
<input type="text" size="10" value="msger.net" name="homepage">
<input type="text" size="10" value="106814" name="qq">
<input type=submit value="复杂get/post方式">
</form>
</body>
</html>

perl部分:

#!c:\perl\bin\perl.exe

$|=1;

print "content-type:text/html\n\n";

print "发送方式:$env{'request_method'}<br>\n";
if(read(stdin,$post,$env{'content_length'})){
print "post得到的数据:$post<br>\n";
}
if($get=$env{'query_string'}){
print "get得到的数据:$get<br>\n";
}

$method="post";

for($i=0;$i<=1;$i++){
foreach(split(/&/,$$method)){
$_=~s/\+//g;
($name,$value)=split(/=/,$_);
$name=~s/%([a-fa-f0-9][a-fa-f0-9])/pack("c",hex($1))/eg;
$value=~s/%([a-fa-f0-9][a-fa-f0-9])/pack("c",hex($1))/eg;
$$method{$name}=$value;
}
$method="get";
}

$method="post";

for($i=0;$i<=1;$i++){
print "hash形式的$method数据遍历:<br>\n";
foreach(keys %{$method}){
print "\$".$method."{".$_."}=$$method{$_}<br>\n";
}
print "<br>\n";
$method="get";
}

exit;

####代码结束####

  好了,我要说的是,搞这个研究究竟有什么意义呢?
   意义是:让你知道,用户提交的数据哪些是用post方式,哪些使用get方式的!
   其实我上面那段perl代码已经包括了很多的技术。你通过阅读就可以知道%get里面放的是用get方式提交的,%post同理!

  如果你对我编写的perl代码感兴趣,欢迎切磋:qq:106814。至于我如何获得ie发送来的请求的,我要说我是用perl编的一个server监听8080端口,我是不是像欧姆一样搞研究大多东西都自己编写(当然,让我编写一个操作系统就有点难度了,不过webserver凑合)?开玩笑呢!

qq:106814
email:hackfan@163.com
personal page:http://www.msger.net/hackfan

 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:CGI应用程序开发基础
· 下一篇:ASP、CGI、ISAPI、ODBC之间的差别
· 使用Perl编写CGI时需要注意的几个问题
· Perl教学 第十一篇 文件系统之三
· 在CGI中实现session的想法和实现
· Perl语言的文字处理模式之一
· 实战 FastCGI(简介)


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077