Http知识

此文章参考自韩顺平老师视频

[b]一、常用HTTP请求消息头(并不是每次请求都会同时存在这些消息头)
1.Accept:text/html,image/*(告诉服务器,我可以接受文本/网页,图片)
2.Accept-Charset:ISO-8859-1(可以接受字符编码ISO-8859-1)
3.Accept-Encoding:gzip,compress(可以接受gzip,compress数据)
4.Accept-Language:en-us,zh-cn(浏览器支持中英文)
5.Host:www.baidu.com(主机为www.baidu.com)
6.If-Modified-Since:Tue,11 Tul 2003 12:34:33(告诉服务器我的缓冲中有这个文件,该文件的时间是...)
7.Referer:http://www.baidu.com/index.jsp(告诉服务器,我来自哪里,该消息头,常用于防止盗连)
8.User-Agent:Mozilla/4.0(浏览器内核)
9.Cookie()
10.Connection:close/keep-alive(保持连接,发完数据后,不关闭连接)
11.Date:Tue,11 Tul 2003 20:34:33(浏览器发送该请求的时间)

二、常用HTTP响应消息头(并不是每次请求都会同时存在这些消息头)
1.Location:http://www.baidu.com(让浏览器重定向到些URL)
2.Server:apacher tomcat(服务端容器)
3.Content-Encoding:gzip(告诉浏览器我使用了gzip)
4.Content-Length:80(告诉浏览器会发送的数据大小80字节)
5.Content-Language:zh-ch(支持中文)
6.Content-Type:text/html;charset=gb2312(内容格式html,支持gb2312编码)
7.Last-Modified:Tue,11 Tul 2003 10:34:33(告诉浏览器,该资源上次更新时间)
8.Refresh:1;url=http://www.baidu.com(过多久,跳转至url指定的路径)
9.Content-Disposition:attachment;filename=aaa.zip(告诉浏览器,有文件下载)
10.Transfer-Encoding:chunked(传输编码)
11.Set-Cookie:SS=Q0=5Lb;path=/search

//下面三个消息头功能一致,如需保证兼容各种浏览器,都设置上
12.Expires:-1(告诉浏览器如何缓存数据,仅支持IE浏览器)
13:Cache-Control:no-cache(告诉浏览器如何缓存数据,)
14:Pragma:no-cache(告诉浏览器如何缓存数据)

15:Connection:close/Keep-Alive(保持连接,发完数据后,不关闭连接)
16.Date:Tue,11 Tul 2003 20:34:33(服务器响应该请求的时间)
[/b]

示例:
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//缓存,永不缓存
response.setDateHeader("Expires", -1);
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");

//缓存一天时间(与上面永不缓存设置不同时存在)
response.setDateHeader("Expires", System.currentTimeMillis() + 3600 * 1000 * 24 );
}


/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//Refresh
//多少时间后跳转到url指定的链接
//url可以指定自己,这样页面就会定时刷新
response.setContentType("text/html;charset=utf-8");
response.setHeader("Refresh", "5;url=http://www.baidu.com");
}


/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//告诉服务器我来自哪里。用于防盗连
//1.如果直接在浏览器用http://localhost:8080/HttpDemo/downFileServlet访问,referer为null
//2.如果写html,内容为<a href="http://localhost:8080/HttpDemo/downFileServlet">文件下载<a/>,referer也为null
//3.如果在其它web项目(访问目录 http://ip:port/example/example.jsp)中访问此servlet,则referer为 http://ip:port/example/example.jsp.
//4.如果在本项目中访问,则referer为 http://localhost:8080/HttpDemo,允许访问
String referer = request.getHeader("Referer");
if(null == referer || !referer.startsWith("http://localhost:8080/HttpDemo")){
response.sendRedirect("error.jsp");
}

response.setContentType("text/html");
//此http头用来文件下载
response.setHeader("Content-Disposition", "attachment; filename=APP_42_619783.udg");
OutputStream out = response.getOutputStream();

String fullPath = this.getServletContext().getRealPath("/images/APP_42_619783.udg");
FileInputStream fis = new FileInputStream(fullPath);
byte[] buff = new byte[1024];
int len = 0;
while((len = fis.read(buff)) > 0){
out.write(buff, 0, len);
}
fis.close();
out.close();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值