http请求与响应

来源:互联网 发布:mac升级flash插件 编辑:程序博客网 时间:2024/05/09 17:20

http请求头:

Accept: text/html,image/*    浏览器通过这个头,告诉服务器它所支持的数据类型

Accept-Charset: 浏览器通过这个头,告诉服务器它采用的字符集

Accept-Encoding:浏览器通过这个头,告诉服务器,它所支持的压缩格式

Accept-Language:浏览器通过这个头,告诉服务器,它所采用的语言

Host:浏览器通过这个头,告诉服务器,我想访问服务器哪台主机

If-Modified-Since:浏览器通过这个头,告诉服务器,它缓存数据时间是多少。

Referer:浏览器通过这个头,告诉服务器,我是从哪个网页点过来的(防盗链)

User-Agent: 浏览器通过这个头,告诉服务器,当前浏览器操作系统的信息,以及浏览器的版本号

Connection:

Date:

 

 

 

http响应头:

Location:这个头通常配合302状态码使用,它用于告诉浏览器你去找谁。

Server:告诉浏览器,服务器的类型

Content-Encoding: 服务器通过这个头,告诉浏览器,回送的数据采用的压缩格式。

Content-Length: 80

Content-Language: zh-cn

Content-Type:这个头用于告诉浏览器,回送数据的类型

Last-Modified:这个头用于告诉浏览器,数据的最后修改时间

Refresh: :这个头用于控制浏览器定时刷新x

Content-Disposition: 用于通知浏览器,以下载方式打开回送的数据

Transfer-Encoding: 用于通知浏览器,数据是以分块形式回送的

ETag: 缓存相头的头

Expires: 用于说明网页的失效时间,如果该值为一个<0的值,则服务器是通知浏览器不要缓存

Cache-Control: no-cache  通知浏览器不要缓存

Pragma: no-cache  

 

 

           示例代码:

1.响应头中的Location(配合302状态码进行重定向)

       response.setStatus(302);

response.setHeader("Location","/Servlet/index.jsp");

 

2.压缩数据后传给浏览器(通过content-encoding和content-length两个头)

Stringdata="abcdefghijklmnopqrstuvwxyz";

System.out.println("压缩前:"+data.getBytes().length);

ByteArrayOutputStream bout=newByteArrayOutputStream();//定义一个字节数组流

GZIPOutputStream gout=new GZIPOutputStream(bout);//告诉gout,把压缩后的数据放到哪,即放到bout中

gout.write(data.getBytes());//gout的write方法把压缩后的数据写到bout中去了

gout.close();//因为gout是缓冲流,如果数据量不是很大,gout会把数据写到缓冲里面去,而不会写到底层了bout去,所以gout                                        //要close一下

byte gzip[]=bout.toByteArray();//得到压缩后的数据

System.out.println("压缩后:"+gzip.length);

response.setHeader("content-encoding","gzip");//通过这个头告诉浏览器数据采用的压缩格式

response.setHeader("content-length",gzip.length+"");//通过这个头告诉浏览器数据的字节长度

response.getOutputStream().write(gzip);//想浏览器写数据

 

 

 

3.将一个图片发送给浏览器

response.setHeader("content-type","image/jpeg");//在Tomcat的conf目录下面的web.xml文件中有常用的mime类型去设置                                                                                                                            //content-type的值

InputStream in=this.getServletContext().getResourceAsStream("/img.jpg");

int len=0;

byte buffer[]=new byte[1024];

OutputStreamout=response.getOutputStream();

while((len=in.read(buffer))>0){

out.write(buffer,0,len);

}

4.Refresh: :这个头用于控制浏览器定时刷新

   response.setHeader("refresh","3");//每隔3秒定时刷新

   response.setHeader("refresh","3;url='http://www.baidu.com'");//每隔3秒定时刷新到指定URL上去

 

5. Content-Disposition: 用于通知浏览器,以下载方式打开回送的数据

public voidcontentDisposition(HttpServletResponse response)throws IOException {

 

response.setHeader("content-disposition","attachment;filenale=downfile.jpg");

 

InputStream in =this.getServletContext().getResourceAsStream("/img.jpg");

int len = 0;

byte buffer[] = new byte[1024];

OutputStream out =response.getOutputStream();

while ((len = in.read(buffer)) > 0) {

out.write(buffer, 0, len);

}

}

 

 

 

6.断点续传(用到http请求头中的range字段)

    模拟一个浏览器

   public static void main(String[] args) throws Exception {

URL url=newURL("http://localhost:8080/Servlet/down.txt");

HttpURLConnection conn=(HttpURLConnection) url.openConnection();

conn.addRequestProperty("range","bytes=11-");//bytes不同取值的含义:1-100,从1到第100个字节;100-,100个字节以后                                                                                                 // 的数据;100,传输最后100个字节

 

InputStream in=conn.getInputStream();

int len=0;

byte buffer[]=new byte[1024];

FileOutputStream out=newFileOutputStream("c:\\down.txt",true);

while((len=in.read(buffer))>0){

out.write(buffer,0,len);

}

in.close();

out.close();

}

 

 

7.列举三个控制浏览器不要缓存的头字段,并给出相应的设置值

Expires: 值设置为0或者-1就可以禁止缓存。用于说明网页的失效时间,如果该值为一个<0的值,则服务器是通知浏览器不要缓存

Cache-Control: no-cache  通知浏览器不要缓存

Pragma: no-cache   

原创粉丝点击