您的位置:首页 > 理论基础 > 计算机网络

深入理解HTTP协议

2017-04-02 23:48 281 查看
       用户打开Web浏览器(常见的HTTP客户端),输入URL地址,就能接收到远程HTTP服务器发送过来的网页,即HTTP协议遵循请求(Request)/应答(Response)模型。Web浏览器向Web服务器发送请求,Web服务器处理请求并返回适当的应答。
      Web浏览器常见的有IE、FF、谷歌等浏览器,Web服务器常见有IIS、Tomcat、Jboss等。

      为了深入的了解HTTP协议请求的格式,我们编写一个程序冒充Web服务器,接收一下Web浏览器向Web服务器发送了一些什么信息。
      我们创建一个java项目,创建一个类Test1,实现一个service,读取客户端浏览器的发送给服务器的Http协议的信息。

程序:

package com.ws.demo1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Test1 {

public static ServerSocket server;

/**
* 创建服务器并启动
*
* @param args
*/
public static void main(String[] args) {
Test1 t1 = new Test1();
t1.start();

}

/*
* 启动方法
*/
public void start() {
try {

server = new ServerSocket(9999);
this.receive();

} catch (IOException e) {

e.printStackTrace();
}
}

/*
* 接收数据
*/

private void receive() {
try {

Socket client = server.accept();

// 接收客户端的信息

byte[] data=new byte[20480];
int len=client.getInputStream().read(data);

String requestInfo=new String(data,0,len).trim();
System.out.println(requestInfo);

} catch (IOException e) {

e.printStackTrace();
}
}

/*
* 关闭方法
*/

public void stop() {

try {
server.close();
} catch (IOException e) {

e.printStackTrace();
}
}
}


编写一个HTML文件,login.htm

<html>
<head>
<title>login page</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>

<body>
<form name="form1"  action="本机的9999端口" method="get">
<table border="0">
<tr>
<td>用户名:</td>
<td><input type="text" name="username" id="username"></td>
</tr>
<tr>
<td>密码:<br></td>
<td><input type="password" name="password" id="password"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" value="登录"></td>
</tr>
</table>
</form>
</body>
</html>


【基础知识】

Form :交互数据的表单;

method :请求方式 get/post;

get:数据量小,安全性不高; 默认方式;

post:数据量大,安全性相对高;

action:请求的服务器的路径;

 

id;编号,前端区分唯一性;js常用;

name:名称,后端服务器区分唯一性,获取值,只要只交数据给后台,必须有name;

运行java程序;

在浏览器中打开login.htm;

输入用户名,密码;

GET /login.htm?username=test&password=111&submit=%E7%99%BB%E5%BD%95 HTTP/1.1
Host: localhost:9999
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: zh-CN,zh;q=0.8


【分析】

Get :请求的方式

/login.htm :请求的文件

?username=test&password=111&submit=%E7%99%BB%E5%BD%95 :传递参数

HTTP/1.1:请求的协议版本;

将method方式更改为post,继续测试;

POST /login.htm HTTP/1.1
Host: localhost:9999
Connection: keep-alive
Content-Length: 52
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin: null
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.8

username=test&password=111&submit=%E7%99%BB%E5%BD%95


我们可以看见,post的方式数据是在最后面传递的;同时,我们看见submit的信息也被传递给了服务器,这是一个无效的参数,我们把htm文件中的submit的name删除,再测试一下看看;
POST /login.htm HTTP/1.1
Host: localhost:9999
Connection: keep-alive
Content-Length: 26
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin: null
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.8

username=test&password=111


【结论】:如果需要给服务器,传递信息,必须定义name,不然无法传递给服务器;

通过服务器接收数据,我们了解了get和post两种方式的区别。

现在,我们的冒牌服务器什么也没有给客户端返回,下面我们抓取一个正常的返回信息看一下:

HTTP/1.1 200 OK(CRLF)
Server:server2.0(CRLF)
Date: Sun, 02 Apr 2017 16:08:01 GMT(CRLF)
Content-type:text/html;charset=GBK(CRLF)
Content-Length: 210(CRLF)
(CRLF)
<html>
<head>
<title>HTTP响应事例</title>
</head>
<body>Hello Htserver!
</body>
</html>


【说明】

http1.1 执行的协议 ;

200 是状态码,表示成功;

Server :服务器;

Date:时间;

Content-type:格式;

Content-Length: 210 :长度;

这样,我们对HTTP协议有了更深的了解。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  http协议