版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34928644/article/details/82142029
了解 请求头 的格式和 响应头 的格式
Http请求的格式:
Http请求有三部分组成:
请求首行 GET /index.jsp?name=Jack HTTP/1.1
请求头 …..
[空行]
请求的正文。name=Jack&age=90
POST /a.html HTTP/1.1 //请求行
Accept: */* //客户端支持的返回类型
Referer: http://localhost:2046/b.html //客户端的网址
Accept-Language: zh-cn //客户端的语言类型
User-Agent: Mozilla/4.0 //客户端的浏览器类型
Accept-Encoding: gzip, deflate //客户端支持的数据压缩类型
Host: localhost:2046 //服务器的地址
Connection: Keep-Alive //是否保持连接,可选的值为Keep-alive|close
[必须的一行空行]
name=itcast&pwd=1234 //请求的正文,如果是POST请求则
请求头讲解:
Accept: */* 说明支持的数据类型
Referer: http://localhost:9999/itcast/a.html //从哪儿来的,可以防止非法引用连接
Accept-Language: zh-cn //支持的语言
User-Agent: Mozilla/4.0 //说明使用的浏览器的版本信息
Accept-Encoding: gzip, deflate //客户端支持压缩格式
Host: localhost:9999 //访问的主机
Connection: Keep-Alive //在请求完成后是否断开连接。可选值另有,HTTP/1.1 :close
Cookie : //缓存在客户端的数据(数据量最大4K)
Content-Length: 8 //发送的字节长度
Cache-Control: no-cache //是否缓存数据
Content-Type: application/x-www-form-urlencoded //内容类型,此意为表单
http响应的格式:
HTTP响应的格式也有三部分组成:
响应首行,也叫状态行。 HTTP/1.1 404(状态码) not found
响应头
[空行]
响应正文(Response Content)
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=D6241588E2D14A562CB5BD6ECD0B3DF3
Content-Type: text/html;charset=GBK
Content-Length: 200
Date: Mon, 30 May 2011 09:02:03 GMT
<html><head>Itcast<head>…
响应头讲解:
HTTP/1.1 200 OK //状态码200是正常返回数据
Location:http://localhost:8080/aa/abc.jsp //跳到哪个页面即重定向。等同于sendRedirect(),状态码为302
Server: Apache-Coyote/1.1 //服务器类型
Content-Type: text/html //正文类型
Content-Length: 8 //返回字节的长度
Content-Encoding:gzip //服务器端的压缩格式,用于通知客户端如何解压
Date: Tue, 24 May 2011 10:42:28 GMT
Refresh:1;url=http://www.baidu.com //1秒钟以后刷新到某个地址
Content-Disposition:attchment;filename=“car.jpg” //下载文件时指定下载的文件
Transfer-Encoding: chunked //数据是一块一块的方式发送给客户端的
Expires:-1 //不需要缓存本次响应的数据
Cache-control:no-cache //针对不同版本的浏览器所以必须三个都用
Pragma:no-cache
扫描二维码关注公众号,回复:
2916394 查看本文章
Referer参数
应用场景:
实现防止用户盗用链接(防止用户通过url直接访问),如果不是在本站内访问都阻止访问。
代码演示:
package cn.hncu.servlets;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/sms") //访问路径
public class ShowMsgServlets extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String msg = "";
String referer = req.getHeader("Referer");
if ( referer == null ) {
msg = "不能直接访问改路径的内容";
}else {
if( referer.contains("127.0.0.1") || referer.contains("localhost") ) {
msg = "消息内容......";
}
}
//设置响应头
resp.setHeader("Content-Type", "text/html;charset=utf-8");
resp.getWriter().println(msg);
}
}
Content-Encoding参数
应用场景:
把文本内容压缩后发送给客户端,节省用户流量,加快传输。
过程:
通过java.util.zip.GZIPOutputStream 类 把响应内容压缩,在通过设置响应头的Content-Encoding参数告知浏览器内容的经过gzip压缩过。
代码演示:
package cn.hncu.servlets;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class GZIPOutputStreamServlet
*/
@WebServlet("/gzip") //访问路径
public class GZIPOutputStreamServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String context = "湖南城市学院是一所由湖南省人民政府主办的全日制普通本科院校。学校位于长江中下游平原、洞庭湖南岸环湖生态经济圈核心城市——湖南省益阳市,学校人文底蕴深厚,自然环境优美,交通便捷,距省会长沙中心城区仅55公里.学校前身为1970年创办的益阳地区师范专科学校和1978年创办的益阳基础大学。2002年3月,经教育部批准,当时的益阳师范高等专科学校和湖南城建高等专科学校合并为湖南城市学院,是内陆地区第一所以“城市”命名的全日制公办本科学校。学校遵循“品学兼修,知行统一”的校训精神,努力办人民满意的大学,2012年通过了教育部本科教学工作合格评估,2014年成为“教育部信息化建设试点单位”,是“湖南省文明单位”“湖南省平安高校”";
//技术入口:java.util.zip.GZIPOutputStream 类
//内存流,用于存储压缩后的字节数据
ByteArrayOutputStream baos =new ByteArrayOutputStream();
GZIPOutputStream gzipOut = new GZIPOutputStream(baos);
//把 context转化成字节数据,再通过gzipOut.write进行压缩数据
byte[] src = context.getBytes();
gzipOut.write( src );
//写完必须关流,关流时会自带把数据刷到 内存流中:baos
gzipOut.close();
//获取压缩后的字节数据
byte[] dest = baos.toByteArray();
System.out.println( "原资源的长度:" + src.length );
System.out.println( "压缩后资源的长度:" + dest.length );
//接下来把压缩后的数据响应给客户端
//首先得设置 响应头,告诉浏览器如何解析该响应内容
resp.setHeader("Content-Encoding", "gzip");
resp.setHeader("Content-Type", "text/html;charset=utf-8");
//再把数据通过字节流输出给客户端
resp.getOutputStream().write(dest);
}
}
state状态码:
一些常用的状态码:
状态码都是用一个三位数来表示。
200表示响应成功。
302重定向到其他资源。
404:找不到页,403:访问被拒绝,
401:未认证的用户
405不支持的请求方式,通常在继承了HttpServlet但没有实现doGet或doPost时出现。
400:错误的请求。
500内部错误,如编码或Exception.
所有的状态码,在HttpServletResponse中使用一个静态常量来表示,见Servlet-Api。
Content-Type和Content-Disposition参数
应用场景:
Content-Type告诉浏览器文本内容的类型 */* ,如 text/html、text/javascript或application/force-downlaod(内容用于下载)
Content-Disposition配合Content-Type:application/force-downlaod ,实现下载。
代码演示:
package cn.hncu.servlets.go;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class DownloadServlet
*/
@WebServlet("/download")
public class DownloadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置响应内容强制下载,并且编码是utf-8的编码
resp.setHeader("Content-Type", "application/force-downlaod;charset=utf-8");
//设置下载文件的文件名
String fileName = "a.txt";
resp.setHeader("Content-Disposition","attachment;filename='"+fileName+"'");
//下面输出的内容就是下载的资源
resp.getWriter().println("Hello world!中文...");
}
}