浅谈-----HTTP那些事
来源:互联网 发布:伟尔金融软件 编辑:程序博客网 时间:2024/04/30 10:37
浅谈HTTP协议
一、什么是HTTP协议?
HTTP协议:(HyperTextTransferProtocol)
- 超文本传输协议,是关于如何在网络上传输超级文本HTML的协议。
- 在分层的网络体系结构中,HTTP协议属于应用层,建立在TCP/IP传输层协议的基础上。
- HTTP协议默认使用80端口。
- HTTP1.1版本是目前广泛使用的。
- 应用层协议。
- 主机IP地址或域名。
- 资源所在的路径或是文件名。
二、客户机通过HTTP与服务器的通讯。
三、HTTP请求/响应格式。
HTTP请求类型:
常见的请求方式包括:
- GET-最为常见,但是发送的数据量很小,发送的数据直接包含在URL的后面。
- POST-包含大量数据,数据在请求正文中通过表单进行提交。
GET与POST请求的区别:
- GET:发送到服务器的数据出现在URL的后面,最多不超过1K。
- POST:发送到服务器的数据出现在请求的正文部分,数据量不受限制。
如:<form name=“dd” method=“post” action=“some.jsp”> Name:<input type=“text” name=“name”/> <input type=“submit” value=“提交”/> </form>
Http请求格式(三部分):
- 请求首行 GET /index.jsp?name=Jack HTTP/1.1
- 请求头
- 请求正文 name=Jack&age=90
- 请求头讲解(所有信息都在 request中包含):
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
Cookie: //缓存在客户端的数据(数据量最大4K)
Content-Length: 8 //发送的字节长度
Cache-Control: no-cache //是否缓存数据
Content-Type: application/x-www-form-urlencoded//内容类型,此意为表单
[必须的一行空行]
name=itcast&pwd=1234 //请求的正文,如果是POST请求则
Http响应的格式(三部分):
- 相应首行,也叫状态行
- 响应头。
- [空行]
- 响应正文 (Response Context)。
- 相应头讲解。
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
四、一些常见的状态码:
- 200表示响应成功。
- 302重定向表其他资源。
- 404:找不到页,403:访问被拒绝,
- 401:未认证的用户
- 405不支持的请求方式,通常在继承了HttpServlet但没有实现doGet或doPost时出现。
- 400:错误的请求。
- 500内部错误,如编码或Exception.
五、设置页面不缓存
- 如果你是jsp页面则:
•response.setHeader("Expires","-1");
•response.setHeader("Cache-control","no-cache");
•response.setHeader("Pragma","no-cache");
•%>
- 如果你是Servlet在响应前设置:
•resp.setHeader("Pragma","no-cache");
•resp.setHeader("Cache-Control","no-cache");
- 操作方法:
•刷新不会从缓存中获取数据,刷新时总是向服务器获取新的数据。
六、Content-Encoding使用压缩的数据
<span style="font-size: 18px; white-space: pre;"></span><span style="font-size:18px;">//1、声明输出流<span style="white-space: pre;"></span>ByteArrayOutputStream out = new ByteArrayOutputStream();<span style="white-space: pre;"></span>//2、声明压缩流<span style="white-space: pre;"></span>GZipOutputStream zipout = new GZipOutputStream(out);<span style="white-space: pre;"></span>//3、向输出流压缩数据,此时数据写入到out中<span style="white-space: pre;"></span>zipout.write(“someString”.getBytes(“utf-8”));<span style="white-space: pre;"></span>Zipout.close(); //不要忘了关闭<span style="white-space: pre;"></span>//4、向客户端发送数据<span style="white-space: pre;"></span>设置响应头:<span style="white-space: pre;"></span>resp.setHeader("Content-Encoding","gzip");<span style="white-space: pre;"></span>resp.setHeader("Content-Length",""+b.length);<span style="white-space: pre;"></span>resp.setHeader("Content-Type","text/html;charset=UTF-8");<span style="white-space: pre;"></span>response.getOutputStream().write(out.toByteArray());<span style="white-space: pre;"></span>//使用场景:<span style="white-space: pre;"></span>//限制流量的客户端</span>
七、Content- Disposition响应头下载
用于指定是否下载资源。
<span style="font-size: 18px; white-space: pre;"></span><span style="font-size:18px;">resp.setContentType("application/force-download");<span style="white-space: pre;"></span>resp.setHeader("Content-Disposition","attachment;filename=\""+name+"\"");<span style="white-space: pre;"></span>//如果是中文要进行编码<span style="white-space: pre;"></span>String name = "我的图片.jpg“;<span style="white-space: pre;"></span>name = URLEncoder.encode(name,"UTF-8");<span style="white-space: pre;"></span>//以下使用IO读取文件<span style="white-space: pre;"></span>InputStream in = new FileInputStream(path);<span style="white-space: pre;"></span>byte[] b = new byte[1024];<span style="white-space: pre;"></span>int len = 0;<span style="white-space: pre;"></span>OutputStream out = resp.getOutputStream(); <span style="white-space: pre;"></span> while((len=in.read(b))!=-1){ <span style="white-space: pre;"></span> out.write(b,0,len);<span style="white-space: pre;"></span>}</span><span style="font-size: 18px;"></span>
1 0
- 浅谈-----HTTP那些事
- HTTP AUTH 那些事
- http-url那些事
- HTTP 的那些事
- 关于http的那些事
- php curl http 那些事
- [HTTP那些事] JSON数据
- HTTP 2.0的那些事
- HTTP 2.0的那些事
- HTTP 2.0的那些事
- HTTP 2.0的那些事
- HTTP 2.0的那些事
- 有关http的那些事
- http-proxy配置那些事
- 浅谈矩阵快速幂的那些事
- HTTP浅谈
- http浅谈
- 浅谈Http
- HTML5之FileReader的使用
- java基础二
- J2EE开发 MVC 以OA系统为例
- 【2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest L】【水题】Lucky Chances (点,方向)所经权值都严格小
- mysql 几个有用 sql 语句
- 浅谈-----HTTP那些事
- nginx的基本配置和虚拟主机的配置
- 初学python转义字符
- 解决VS2010链接错误:LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- Hello Mr.J——请把我埋在这Spring里
- 装饰器设计模式
- C语言关键字
- HDU ACM 1050 Moving Tables
- DelegatingActionProxy