一、GET方法
1.服务器设置
在服务器添加g.php文件,用以获取数据请求,并返回数据响应。
示例 g.php,直接返回请求内容。
<?php echo $_GET["txt"]; ?>
2.客户端方法
通过TCP连接服务器的80端口,发送HTTP请求数据。
GET /g.php?txt=123 HTTP/1.1 Host: www.dimmye.com
注意:最后必须是换行。
3.通信测试
在万网虚拟主机得到的响应:
HTTP/1.1 200 OK Date: Sun, 05 Jul 2015 05:49:05 GMT Server: Apache Vary: User-Agent,Accept-Encoding Transfer-Encoding: chunked Content-Type: text/html 3 123 0
在OpenShift得到的响应:
HTTP/1.1 200 OK Date: Sun, 05 Jul 2015 05:48:44 GMT Server: Apache/2.2.15 (Red Hat) Content-Length: 3 Content-Type: text/html Vary: Accept-Encoding 123
区别:
- 1.数据长度(16进制)跟在空换行后面;数据在第二行,长度如上指定;数据结束后有字符0,最后空换行。
- 2.有Content-Length字段,则数据跟在空换行后面,长度已指定。
二、POST方法
1.服务器设置
在服务器添加g.php文件,用以获取数据请求,并返回数据响应。
示例post.php,直接返回请求内容。
id:<?php echo $_POST["id"]; ?>; value:<?php echo $_POST["value"]; ?>
2.客户端方法
通过TCP连接服务器的80端口,发送HTTP请求数据。
POST /post.php HTTP/1.1 Host: www.dimmye.com Content-Type: application/x-www-form-urlencoded Content-Length: 16 id=123;value=456
注意:Content-Length字段指定数据长度(10进制),后跟一空换行,然后数据内容,最后必须是换行代表结束。
3.通信测试
在万网虚拟主机得到的响应:
HTTP/1.1 200 OK Date: Sun, 05 Jul 2015 06:02:12 GMT Server: Apache Vary: User-Agent,Accept-Encoding Transfer-Encoding: chunked Content-Type: text/html 11 id:123; value:456 0
在OpenShift得到的响应:
HTTP/1.1 200 OK Date: Sun, 05 Jul 2015 06:02:00 GMT Server: Apache/2.2.15 (Red Hat) Content-Length: 17 Content-Type: text/html Vary: Accept-Encoding id:123; value:456
区别:
- 1.数据长度(16进制)跟在空换行后面;数据在第二行,长度如上指定;数据结束后有字符0,最后空换行。
- 2.有Content-Length字段,则数据跟在空换行后面,长度已指定。