`
carge
  • 浏览: 50331 次
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

HTTP协议发送数据到服务器端的若干种形式

 
阅读更多

 

 

HTML页面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>HTTP协议学习</title>
</head>
<body>
<form action="test1.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
  <input name="field_1" type="text" value="123" />
  <br/>
  <input name="field_1" type="text" value="456" />
  <br/>
  <input name="field_2" type="text" value="789" />
  <br/>
  <input type="file" name="upload_file[]" />
  <br/>
  <input type="file" name="upload_file[]" />
  <br/>
  <input type="submit" name="Submit" value="submit" />
</form>
</body>
</html>

 

PHP页面:

<?
print_r($_FILES);
print_r($_POST);
?>

 

 

1. 当表单method为POST,并且enctype为multipart/form-data情况

 

1.1 请求数据

POST /go/test/test1.php HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Referer: http://localhost/go/test/test.php
Accept-Language: zh-cn
Content-Type: multipart/form-data; boundary=---------------------------7db1192f70f20
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; InfoPath.2)
Host: localhost
Content-Length: 906
Connection: Keep-Alive
Cache-Control: no-cache

-----------------------------7db1192f70f20
Content-Disposition: form-data; name="field_1"

123
-----------------------------7db1192f70f20
Content-Disposition: form-data; name="field_1"

456
-----------------------------7db1192f70f20
Content-Disposition: form-data; name="field_2"

789
-----------------------------7db1192f70f20
Content-Disposition: form-data; name="upload_file[]"; filename="D:\text_file1.txt"
Content-Type: text/plain

This is content of the text file 1.

Other files are same to text file.
-----------------------------7db1192f70f20
Content-Disposition: form-data; name="upload_file[]"; filename="D:\text_file2.txt"
Content-Type: text/plain

This is content of the text file 2.

Other files are same to text file.
-----------------------------7db1192f70f20
Content-Disposition: form-data; name="Submit"

submit
-----------------------------7db1192f70f20--

 

1.2 响应数据

HTTP/1.1 200 OK
Date: Thu, 27 Jan 2011 03:32:47 GMT
Server: Apache/2.2.15 (Win32) PHP/5.2.13
X-Powered-By: PHP/5.2.13
Content-Length: 849
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html

Array
(
    [upload_file] => Array
        (
            [name] => Array
                (
                    [0] => text_file1.txt
                    [1] => text_file2.txt
                )

            [type] => Array
                (
                    [0] => text/plain
                    [1] => text/plain
                )

            [tmp_name] => Array
                (
                    [0] => C:\WINDOWS\Temp\php14B.tmp
                    [1] => C:\WINDOWS\Temp\php14C.tmp
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                )

            [size] => Array
                (
                    [0] => 73
                    [1] => 73
                )

        )

)
Array
(
    [field_1] => 456
    [field_2] => 789
    [Submit] => submit
)

 

1.3 简要分析说明

(1)  在消息头中存在一个“Content-Type: multipart/form-data; boundary=---------------------------7db38a3570f20 ",其中的boundary是用来告诉程序如何把消息体中各个字段分开;

(2)  HTTP协议发送的时候,并不关心各个field的name,即允许一次发送中有多个field有相同的名字。PHP处理这种数据时,如果名字相同且名字以 “[]”结尾,则会把这些数据放入一个数组中(见upload_file);如果名字相同但不是以“[]”结尾,则后面一个field会覆盖前面的field (见field_1);

(3) 发送文件的时候是直接把文件内容放在该区域的boundary区域。

 

 

2. 当表单method为POST,并且enctype为application/x-www-form-urlencoded情况

 2.1 请求数据 

POST /go/test/test1.php HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Referer: http://localhost/go/test/test.php
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; InfoPath.2)
Host: localhost
Content-Length: 129
Connection: Keep-Alive
Cache-Control: no-cache

field_1=123&field_1=456&field_2=789&upload_file%5B%5D=D%3A%5Ctext_file1.txt&upload_file%5B%5D=D%3A%5Ctext_file2.txt&Submit=submit

 

 2.2 响应数据 

HTTP/1.1 200 OK
Date: Thu, 27 Jan 2011 03:51:56 GMT
Server: Apache/2.2.15 (Win32) PHP/5.2.13
X-Powered-By: PHP/5.2.13
Content-Length: 209
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html

Array
(
)
Array
(
    [field_1] => 456
    [field_2] => 789
    [upload_file] => Array
        (
            [0] => D:\\text_file1.txt
            [1] => D:\\text_file2.txt
        )

    [Submit] => submit
)

 

2.3 简要分析

(1) 表单为POST方式的时候,当enctype为空时,enctype默认为“application/x-www-form-urlencoded”;

(2) 此时的文件域数据被当作普通数据来处理;

(3) 此时发送数据不是放在boundary之中而是组合成一个URL queryString的形式;

(4) 所有的field_name和field_value都被URL编码。

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics