导图社区 HTTP 协议
HTTP 协议知识思维导图,包括:http协议定义、http版本1.0与1.1区别、http请求方式get/post区别、http消息请求头、Http响应头信息、文件下载案例、处理浏览器缓存。
编辑于2022-06-25 23:05:40HTTP 协议
http协议定义
1、http协议全称:超文本传输协议 2、http协议是建立在TCP/IP协议基础之上建立的
http版本1.0与1.1区别:
Http1.0短连接 http1.1长连接 所谓的长和短指的是:时间的长连接1.1版本大概是30秒,短连接是发送完就断开连接
http请求方式get/post区别:
1、传输的方式:get请求方式发送时是在url地地址中发送,而post请求提交时是从包体中发送 2、传输大小:http协议没有传输大小的限制,特定的浏览器,服务器有限制,例如I.E.限制(2k+35) 而fireFox理论上没有限制,有限制取决于操作系统 3、安全性:post提交安全,get提交相对不安全
http消息请求头
客户端浏览器向服务器发送请求request
请求头信息: POST /rcfb/user/getLogin.zealinfo HTTP/1.1-->请求行 Get/Post Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, */*-->告诉服务器,我可以接收文本、图片,网页等信息; Referer: http://localhost:8080/rcfb/index.zealinfo-->告诉服务器是从哪里来的,可以该上盗链,防下载; Accept-Language: zh-CN-->接收字符编码 User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Tablet PC 2.0)-->告诉浏览器内核 Content-Type: application/x-www-form-urlencoded Accept-Encoding: gzip, deflate-->可以接收gzip, deflate压缩之后的数据 Host: localhost:8080-->我要找的主机是 localhost:8080 Content-Length: 46 Connection: Keep-Alive-->保持连接,发送完数据,我不关闭连接; Cache-Control: no-cache-->缓存 Cookie: JSESSIONID=FFEA458426B435CB09B75D6116F74BFE userAdd.userName=lihaoran&userAdd.passWord=123-->包体内容
获取请求代码演示: public void goHeaderInfo() throws IOException{ ActionContext context = ActionContext.getContext(); HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE); HttpSession session = ServletActionContext.getRequest().getSession(); response.setCharacterEncoding("UTF-8");//将这段返回页面信息代码转换成utf-8 response.setContentType("text/html;charset=UTF-8");//页面要以utf-8形式显示 String accept=request.getHeader("Accept"); String referer=request.getHeader("Referer"); String host=request.getHeader("Host"); String charet=request.getHeader("Accept-Encoding"); String cookie=request.getHeader("Cookie"); String Connection=request.getHeader("Connection"); String cache=request.getHeader("Cache-Contro"); String agent=request.getHeader("User-Agent"); String date=request.getHeader("Date"); String project=ServletActionContext.getServletContext().getContextPath();//获取项目名 //防盗链 告诉服务器我来自己哪里 常用在防盗链referer: http://localhost:8080/SSH_Base_jdbcTempte_session_XML20/pages/test/daolian.jsp if(referer==null||!referer.startsWith("http://localhost:8080"+project)){ response.sendRedirect(project+"/say/errors.lihaoran"); return; } response.getWriter().println("告诉服务器我可以接收的内容accept:"+accept+"<br/>"); response.getWriter().println("可接收的压缩格式:Accept-Encoding\t "+charet+"<br/>"); response.getWriter().println("告诉服务器我来自己哪里 常用在防盗链referer:\t "+referer+"<br/>"); response.getWriter().println("主机地址Host:\t"+host+"<br/>"); response.getWriter().println("保持连接,发完数据不关闭链接Connection:\t"+Connection+"<br/>"); response.getWriter().println("告诉服务器浏览器内核User-Agent:\t"+agent+"<br/>"); response.getWriter().println("缓存Cache-Contro:\t"+cache+"<br/>"); response.getWriter().println("浏览器发送http请求时间Date:\t"+date+"<br/>"); response.getWriter().println("cookie:\t"+cookie+"<br/>"); }
Http响应头信息
服务器响应浏览器请求信息
响应头信息: HTTP/1.1 302 Moved Temporarily-->状态行200表示成功; 302表示让浏览器转到别一个链接上(redirectAction ),404表示错误;500服务器错误; Server: Apache-Coyote/1.1-->告诉浏览器 我的服务是tomact Location: http://localhost:8080/rcfb/user/list.zealinfo-->定位到另一个url地址 Refresh: url=http://localhost:8080/rcfb/index.zealinfo-->告诉浏览器多久之后刷新到index.zealinfo页面上 Content-Type: text/html;charset=UTF-8-->内容文本格式,字符UTF-8字符 content-Encoding:gzip-->告诉浏览器,我是以gzip的压缩后传输的 Content-Length: 0 Content-Disposition: filename="2013-01-21 11-11-59.doc"-->告诉浏览器有文件要下载 set-cookie:JSESSIONID=FFEA458426B435CB09B75D6116F74BFE--> Last-Modified: Wed, 13 Feb 2013 12:50:35 GMT-->告诉浏览器,上次更新时间是... Date: Wed, 13 Feb 2013 15:51:55 GMTopic
//底层写法 服务器跳转 response.setStatus(302); response.setHeader("Location","say/getInfo2.lihaoran"); //两者等同 //现实用法 response.sendRedirect("say/getInfo2.lihaoran"); //分钟 自动刷新跳转到url新链接上去 //response.setHeader("Refresh","5;url=say/getInfo2.lihaoran");
文件下载案例
//文件下载 public void getDownload() throws IOException{ ActionContext context = ActionContext.getContext(); HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE); //防盗链 String referer=request.getHeader("Referer"); String project=ServletActionContext.getServletContext().getContextPath();//获取项目名 //防盗链 告诉服务器我来自己哪里 常用在防盗链referer: http://localhost:8080/SSH_Base_jdbcTempte_session_XML20/pages/test/daolian.jsp if(referer==null||!referer.startsWith("http://localhost:8080"+project)){ response.sendRedirect(project+"/say/errors.lihaoran"); return; } //处理下载后中文乱码 String temps = java.net.URLEncoder.encode("简历.rar","utf-8"); String realpath; realpath = ServletActionContext.getServletContext().getRealPath(File.separator+"DownLoad" +File.separator+ "简历.rar") ; response.setHeader("Content-Disposition","attachment;filename="+temps); //文件输入流 FileInputStream fis = new FileInputStream(realpath); byte buff[]=new byte[1024]; int len=0; OutputStream os = response.getOutputStream(); while((len=fis.read(buff))>0){ os.write(buff,0,len); } //关闭链接 os.close(); fis.close(); }
处理浏览器缓存
//缓存 public void cacheInfo() throws IOException{ ActionContext context = ActionContext.getContext(); HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE); response.setCharacterEncoding("UTF-8");//将这段返回页面信息代码转换成utf-8 response.setContentType("text/html;charset=UTF-8");//页面要以utf-8形式显示 //指定页面不缓存 response.setDateHeader("Expires",-1); //保证兼容 response.setHeader("Cache-Control","no-cache"); response.setHeader("Pragma","no-cache"); //设置缓存一天失效 //response.setDateHeader("Expires",System.currentTimeMillis()+3600*1000*24); response.getWriter().print("当前时间"+new java.util.Date().toLocaleString()); }
处理中文乱码
//处理乱码工具类 public static String getStringEncode(String encode) throws UnsupportedEncodingException{ return new String(encode.getBytes("iso-8859-1"),"utf-8"); } username =EncodeTool.getStringEncode(this.username);
Struts中以redirectAction 页面提交方法是post 方式时 乱码解决方法action.xml <!-xml配置方法--> <action name="getEcode" class="com.action.session.SessionAction" method="getEcode"> <result name="success" type="redirectAction"> <param name="actionName">getEcode2.lihaoran</param> <param name="username">${username}</param> <param name="passworld">${passworld}</param> </result> </action> <!-- 解决中乱码--> <action name="getEcode2" class="com.action.session.SessionAction" method="getEcode2"> <result name="success" type="dispatcher">/pages/test/encodeList.jsp</result> </action>
getEcode()方法 public String getEcode() throws UnsupportedEncodingException{ HttpServletRequest request = ServletActionContext.getRequest(); //对参数不做任何处理 return SUCCESS; }
getEcode2()方法 public String getEcode2() throws Exception{ HttpServletRequest request = ServletActionContext.getRequest(); //需要转一次码 username =EncodeTool.getStringEncode(this.username); passworld = EncodeTool.getStringEncode(this.passworld); return SUCCESS; }
Struts中以redirectAction 页面提交方法是超链接或get方式提交时 需转两次码 解决方法如下:acton.xml文件同上 Action.java //处理中文乱码 两次转码 public String getEcode() throws UnsupportedEncodingException{ HttpServletRequest request = ServletActionContext.getRequest(); //重写向前 转码成 utf-8 username =EncodeTool.getStringEncode(this.username); passworld = EncodeTool.getStringEncode(this.passworld); return SUCCESS; } //redirectAction 乱码解决方法 public String getEcode2() throws Exception{ HttpServletRequest request = ServletActionContext.getRequest(); //重写向后转码成 utf-8 username =EncodeTool.getStringEncode(this.username); passworld = EncodeTool.getStringEncode(this.passworld); return SUCCESS; }