http协议简介

cmd
telnet localhost 8080
GET /aa/1.html HTTP/1.1
Host:

 来访问页面
 
给IE下安装httpwatch可以看到http协议

什么是http协议:超文本传输协议,他是TCP/IP协议的一个应用层协议,用于定义web浏览器和web服务器之间的交互数据的过程。

http协议是学习javaweb开发的基石

HTTP协议版本:http/1.0(客户端和服务器建立连接后只能获取一个资源)、http/1.1(允许客户端和服务器建立连接后获取多个资源)

WEB页面开发原则:尽量的减少HTTP请求数

http请求格式:

GET /aa/1.html HTTP/1.1	(请求行:)

以下为多个请求头:
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg,
 		application/x-ms-xbap, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*(告诉服务器支持哪些数据类型)
Accept-Language: zh-CN(语言环境)
Accept-Charset:(客户机支持的编码)
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2;
			.NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)(告诉服务器,客户机的软件环境) 
Accept-Encoding: gzip, deflate(客户机支持的压缩格式,压缩传递)
If-Modified-Since: Sun, 08 Mar 2015 01:56:15 GMT(告诉服务器缓存资源的时间,若服务器网页没用更新,直接使用缓存在客户机的页面。否则从新发送html页面)用处:减轻服务器压力
If-None-Match: W/"11-1425779775033"
Host: localhost:8081(客户 机告诉服务器,想访问的主机名)
Connection: Keep-Alive/close(请求完后,保持连接还是关闭连接)
Referer:(请求前的资源页面(从哪个资源(页面)跳到此页面))用处:防盗链(查看referer的前半部分是否是本网站的资源,判断是否有权限打开该页面)
Cookie:(客户机通过此头可以给服务器带数据)
Range:(Range头可实现断点续传功能。Range:bytes=1000-2000。传输范围从1000-2000,Range:bytes=1000-。传输范围为1000个字节以后的数据。Range:bytes=1000,传输最后的1000个字节)
Date:(当前时间值)

public static void  main(String[] args){
	URL url = new UTL("http://localhost:8081/aa/1.txt");
	HttpUrlConnection conn = (HttpUrlConnection)url.openConnection();
	
	conn.setRequestProperty("Range","bytes=5-");
	
	InputStream in = conn.getInputStream();
	int len;
	byte[] buffer = new byte[1024];
	
	FileOutputStream out = new FileOutputStream("c://a.txt",true)
	while((len = in.read(buffer))>1024){
		out.write(buffer , 0 , len);
	}
	in.close;
	out.close;
}



http响应:

http状态头:
HTTP/1.1 304 Not Modified(状态行:描述服务器对客户机的处理结果 , 状态码:200表示:响应无误。302:告诉服务器去找另外地址 304,307:告诉客户机去浏览器找缓存 403:用户没有权限访问该资源 404:客户端的请求有误 500:服务器端出现错误)

以下为多个响应头:(服务器回送的响应头可以控制客户端浏览器以何种方式来处理服务器回送的文件(打开?下载?))
Location:http://www.it1315.org/index.jsp (这个头配合302状态码使用,告诉浏览器去访问http://www.it1315.org/index.jsp 即会跳转到Location页面下的地址中去(即为请求重定向))
Server: Apache-Coyote/1.1(告诉浏览器服务器类型)
Content-Encoding:gzip(服务器告诉浏览器,数据的压缩格式) 服务器压缩数据能提高整个服务器的性能:(页面打开速度加快,减少流量)
Content-Length:告诉浏览器回送数据的长度
Content-Language:zh-cn告诉浏览器回送的语言环境
Last-Modified:服务器通过该字段告诉浏览器当前资源缓存时间
Refresh:服务器通过此头,告诉浏览器隔多长时间刷新一次
Content-Disposition:服务器通过此头告诉浏览器以下载的方式打开该文件
Content-Type:text/html;charset=GB2312(服务器通过此头,设置文件的类型(文字?图片?其他?))
Transfer-Encoding:服务器通过此头告诉浏览器数据的传送的格式(块传输?)
Cookie:保存数据
ETag: W/"11-1425779775033"(缓存相关的头:服务器根据数据的内容生成一个唯一的标识符,数据改变,标识符也会跟着改变。(若标识符未变,浏览器从缓存中取页面(可做到时时更新(比Modified更精切))))
Expires:服务器通过这个头告诉浏览器把该资源缓存多长时间(-1和0,表示不缓存。)
(以下两个头都是告诉浏览器不要缓存数据,历史和浏览器类型原因(写程序时建议把这三个头都设置为不缓存)。)(缓存内容:图片(不发生变化的数据资源)。不缓存内容:实时性很强的数据,变化很大的数据)
Cache-Control:no-cache
Pragma:no-cache
Connection:close/keep-Alive(相应完后是否断开连接)
Accept-Ranges:bytes/none(服务器是否支持Range断点续传(bytes:支持,none:不支持))
Content-Range:1000-3000/5000(返回1000-3000个字节,总字节数为5000)(常用作客户端程序)
Date: Sun, 08 Mar 2015 06:58:00 GMT


Servlet网页数据压缩:
通过控制Content-Length和Content-Encoding来控制服务器压缩网页数据
public void doGet(HttpServletRequest req , HttpServletResponse rep) throws ServletException ,IOException{
	String data = "xxxxxxxx";
	System.out.println("原始数据大小:"+data.getBytes().length);
	
	ByteArrayOutputStream bout = new ByteArrayOutputStream();
	
	GZIPOutputStream bout = new GZIPOutputStream(bout);
	gout.write(data.getBytes());
	gout.close();//关闭流,把数据全部写入bout中
	
	byte[] gzip = bout.toByteArray();//得到压缩后的结果
	System.out.println("压缩后的结果为:"+gzip.length);
	
	//通知浏览器数据采用的压缩格式
	rep.setHeader("Content-Encoding","gzip");
	rep.setHeader("Content-Length",gzip.length+"");
	
	rep.getOutputStream().write(gzip);
}

Servlet网页数据格式:
//通过控制Content-Type头字段控制网页数据类型。
public void doGet(HttpServletRequest req , HttpServletResponse rep) throws ServletException ,IOException{

	rep.setHeader("Content-Type","image/bmp");
	
	InputStream in = this.getServletContext().getResourceAsStream("/1.bmp");
	int len = 0;
	byte[] buffer = new byte[1024];
	
	OutputStream out = rep.getOutputStream();
	while((len=in.read(buffer))>0){
		out.write(buffer,0,len);
	}
}

//Servlet控制页面每3秒刷新页面一次
//通过控制Refresh来控制页面每隔多长时间请求再次开始(常用在:股票。多人聊天。)
public void doGet(HttpServletRequest req , HttpServletResponse rep) throws ServletException ,IOException{
	//rep.setHeader("refresh","3;url='www.baidu.com'");//3秒后跳转到baidu网站(只跳转一次)
	rep.setHeader("refresh","3);//3秒后再次跳转到本页面(无限循环)
	
	String data="开启javaweb之旅";
	rep.getOutputStream().write(data.getBytes());
}

//下载文件
public void doGet(HttpServletRequest req , HttpServletResponse rep) throws ServletException ,IOException{
	rep.setHeader("Content-Disposition","attachment;fileName=3.jpg");
	
	InpputStream in  = new this.getServletContext().getRescorceAsStream("/3.jpg");
	
	int  len;
	byte[] buffer = new byte[1024];
	
	OuputStream out = new rep.getOutputStream();
	while((len = in.read(buffer))>0){
		out.write(buffer , 0 ,len);
	}
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。