HTTP协议
1.概述
- 超文本传输协议
- 一种分布式、合作式、多媒体信息系统服务
- 面向应用层的协议
- 基于传输层的TCP协议进行通信
- HTTP协议是通用的、无状态的协议
- 功能:在服务器与客户端之间传输超文本
- 网络分层结构
2.特点
- 客户端/服务器模式
- 简单:有效处理大量请求,时间开销小
- 灵活:允许传输任意类型的数据对象(Content-Type指定数据类型)
- 无状态:不会记忆数据,运行速度高,服务器应答速度快
3.事务处理步骤
- 客户端与服务器建立连接
- 客户端向服务器发送请求
- 服务器向客户端回复响应
- 客户端与服务器断开连接
4.类型
- 请求(Request):由客户端向服务器发送消息
组成:请求行,可选的头域、实体
Full-Request = Request-Line
*(General-Header|Request-Header|Entity-Header)
CRLF[Entity-body]
*:0或多
- 响应(Response):服务器回送客户端的消息
组成:状态行、可选的头域、实体
5.请求行
- 格式
Request-Line = Method SP(方法规范)
Request-URl SP(请求资源标识符)
HTTP-Version CRLF(协议版本)
GET http://www.baidu.com/index.html HTTP/1.0
- 请求方法
没有手工写入资源地址
1.www.baidu.com会自动添加
/index.html /index.htm /index.jsp
2.服务器的欢迎页面
GET / HTTP/1.1
Host: www.baidu.com
写入资源地址
www.baidu.com/index.html
GET /index.html HTTP/1.1
有地址栏参数
www.baidu.com/?name=a&age=20
GET /?name=a&age=20 HTTP/1.1
Host: www.baidu.com
接收参数:request.getParameter("username");
浏览器拼接参数
-----------------------------121066605317290619602852684163
Content-Disposition: form-data; name="head"; filename="1.png"
Content-Type: image/png
‰PNG(魔术字)
skdjfldsjfdskfj
-----------------------------121066605317290619602852684163
Content-Disposition: form-data; name="uname"
森林
-----------------------------121066605317290619602852684163--
以魔术字来判断文件类型
不能再用request.getParameter("username");接收参数
6.案例1
https://pm.myapp.com/invc/xfspeed/qqpcmgr/download/QQPCDownload1530.exe
* 得到这个文件的大小,并在本地创建有大小的空文件
* 文件名:时间
* 后缀名:原文件的后缀名
* 目录:用户目录
public class t1_DownLoadPrepare {
public static void main(String[] args) throws Exception {
String url = "https://pm.myapp.com/invc/xfspeed/qqpcmgr/download/QQPCDownload1530.exe";
URL u = new URL(url);
HttpURLConnection con = (HttpURLConnection) u.openConnection();
//请求头Head
con.setRequestMethod("HEAD");
//设置超时时间
con.setConnectTimeout(3000);
//连接
con.connect();
//底层----Socket s = new Socket("https://pm.myapp.com",80);
// HEAD /invc/xfspeed/qqpcmgr/download/QQPCDownload1530.exe HTTP/1.1\r\n\r\n
//获取结果
//文件大小
long fileSize = con.getContentLength();
System.out.println(fileSize);
//文件名
Date d = new Date();
SimpleDateFormat s = new SimpleDateFormat("yyyyMMddHHmmss");
String fileName = s.format(d);
//后缀名
String suffix = url.substring(url.lastIndexOf("."));
String newFileName = fileName + suffix;
//在用户(有权限)目录中创建
String userHome = System.getProperty("user.home");
//File f = new File(userHome, newFileName);//路径 名字
/*if (f.exists() == false) {
file.setLength(fileSize);
file.close();
System.out.println("文件已创建");
}*/
//建一个有大小的空文件
//File.separator == /
RandomAccessFile file = new RandomAccessFile(userHome + File.separator + newFileName, "rw");
file.setLength(fileSize);
file.close();
System.out.println("文件已创建:" + file.getFD());
}
}
7.头域
- 请求头域
Cache:
Client:
Accept:客户端能接收的服务器回送内容的压缩方式
Cookie/Login:**
客户端存在服务器上次响应的cookie信息
Entity:**
发送给服务器内容的类型和长度,POST请求才有
Content-Length Content-Type
Miscelllalneous:
Referer:请求来源
作用:广告计费,防止盗链
Transport:
Connection:** keep-alive Close
Host:主机
User-Agent:**
浏览器类型
- 响应头域
Cache:
Cookie/Login:
Set-Cookie:服务器生产cookie信息,要求客户端保存
----解决HTTP协议的无状态性
Content-Length:文件长度
Content-Encoding:编码方式
Content-Language:语言
Miscelllalneous:
Server:服务器类型
8.响应行
- 格式
协议版本 状态码
- 状态码
1XX:还在处理
2XX:成功
3XX:重发请求
302:重定向
304:要求客户端从本地读取缓存数据
4XX:
404:资源找不到
5XX:
500:服务器错误
9.HTTP传输流程
- 地址解析
输入www.baidu.com
由DNS进行域名解析得到主机IP
- 浏览器封装HTTP数据包:Socket(ip,端口) 拼接应用层HTTP
应用
传输层:端口TCP协议
网络层:IP协议
以太网:以太网协议
- 封装TCP包
- 建立TCP连接:三次握手
- 客户端发送请求
- 服务器响应
- 服务器关闭TCP连接