HTTP请求头与响应头 实例

HTTP协议是学习JavaWEB开发的基石,不深入了解HTTP协议,就不能说掌握了WEB开发!

 

消息头:

l用于HTTP请求中的常用头
•Accept: text/html,image/*   
•Accept-Charset: ISO-8859-1
•Accept-Encoding: gzip,compress
•Accept-Language: en-us,zh-cn
•Host: www.baidu.com

•If-Modified-Since: Tue, 11 Jul 2000 18:23:51 GMT
•Referer: http://www.baidu.org/index.jsp
•User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
•Cookie
•Connection: close/Keep-Alive  
•Date: Tue, 11 Jul 2000 18:23:51 GMT

 

http请求头:

Accept:这个头用于告诉服务器,浏览器支持哪些数据类型
Accept-Charset:这个头用于告诉服务器,浏览器采用的是哪种编码
Accept-Encoding:这个头用于告诉服务器,浏览器支持哪种数据压缩格式
Accept-Language:这个头用于告诉服务器,浏览器的语言环境
Host:这个头用于告诉服务器,浏览器想访问的主机名
If-Modified-Since:这个是和缓存相关的头,这个头用于告诉服务器,浏览器缓存资源的时间
Referer:这个头用于告诉服务器,浏览器是从哪个页面来的(可以防止盗链)
User-Agent: 用于告诉服务器,浏览器的机器环境
Cookie:浏览器通过cookie,可以带一些数据给服务器

-----------------------------------------------------------------------------

Http响应:
location: 这个头配合302使用,用于告诉浏览器去找哪个资源
Server:这个头用于告诉浏览器,服务器的类型
Content-Encoding: 这个头用于告诉浏览器,数据的压缩格式
Content-Length: 这个头用于告诉浏览器,数据的长度
Content-Type:这个头用于告诉浏览器,回送数据的类型
Last-Modified: 这个头用于告诉浏览器,资源的最后修改时间(缓存相关的头)
Refresh:这个头用于控制浏览器定时刷新,
Content-Disposition: 用于控制浏览器以下载方式打开回送的数据
Transfer-Encoding: 用于告诉浏览器,数据的传送方式
Expires: -1
Cache-Control: no-cache 
Pragma: no-cache  
以上三个都可以实现浏览器不要缓存资源数据

 

HTTP请求中的常用响应头
Location: http://www.baidu.org/index.jsp
Server:apache tomcat
Content-Encoding: gzip
Content-Length: 80
Content-Language: zh-cn
Content-Type: text/html; charset=GB2312
Last-Modified: Tue, 11 Jul 2000 18:23:51 GMT
Refresh: 1;url=http://www.baidu.org
Content-Disposition: attachment; filename=aaa.zip
Transfer-Encoding: chunked 
Set-Cookie:SS=Q0=5Lb_nQ; path=/search
ETag: W/"7777-1242234904000"
Expires: -1
Cache-Control: no-cache 
Pragma: no-cache  
Connection: close/Keep-Alive  
Date: Tue, 11 Jul 2000 18:23:51 GMT

---------------------------------------------------------------------------------

Web端口:80开发是8080

Smtp25

Pop3:110

ftp23

https443

jar

优惠原则:尽量减少http请求数。

Accept: 支持

 

响应头

客户端:

302 你向我借钱,我没钱,要你去找谁。

304,307:要你去拿缓存,拿缓存的数据显示

403,米有权限拒绝。

404:你请求的资源web服务器没有。

服务器:

500:服务器出错。

对实时性很高的软件,不能有缓存

ETag:实时缓存。

----------------------------------------------------------------------------------

 在Servlet测试的几段http请求响应代码:

 

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //response.getWriter().write("aaaaaaa"); //得到浏览器传来的数据 System.out.println(request.getMethod()); System.out.println(request.getHeader("name")); System.out.println(request.getParameter("password")); } //向浏览器写出一个图片 private void test5(HttpServletResponse response) throws IOException { InputStream in = this.getServletContext().getResourceAsStream("/2.jpg"); int len = 0; byte buffer[] = new byte[1024]; response.setHeader("content-disposition", "attachment;filename=2.jpg"); OutputStream out = response.getOutputStream(); while((len=in.read(buffer))>0){ out.write(buffer, 0, len); } } //控制浏览器定时刷新 private void test4(HttpServletResponse response) { response.setHeader("refresh", "5;url='http://www.mywangs.com'"); } //向浏览器写出一张图片 private void test3(HttpServletResponse response) throws IOException { response.setHeader("content-type", "image/jpeg"); //mime协议 InputStream in = this.getServletContext().getResourceAsStream("/1.jpg"); int len = 0; byte buffer[] = new byte[1024]; OutputStream out = response.getOutputStream(); while((len=in.read(buffer))>0){ out.write(buffer, 0, len); } in.close(); } //把压缩数据发向浏览器 ,, private void test2(HttpServletResponse response) throws IOException { String data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; System.out.println("压缩前:" + data.getBytes().length); ByteArrayOutputStream bout = new ByteArrayOutputStream(); GZIPOutputStream gout = new GZIPOutputStream(bout); gout.write(data.getBytes()); 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); } //跳转页面,移至下一个页面(重写向) private void test1(HttpServletResponse response) { response.setStatus(302); response.setHeader("location", "/md/1.html"); }

 

2..对http请求和响应的解析----

//连接web资源,解析http响应 @Test public void read() throws Exception{ URL url = new URL("http://localhost:8080/demo/servlet/ServletDemo1"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); System.out.println(conn.getResponseCode()); System.out.println(conn.getHeaderField("Server")); InputStream in = conn.getInputStream(); int len = 0; byte buffer[] = new byte[1024]; while((len=in.read(buffer))>0){ System.out.println(new String(buffer,0,len)); } } //向服务器发送Http请求 @Test public void write() throws Exception{ URL url = new URL("http://localhost:8080/md/servlet/ServletDemo1"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("name", "flx"); OutputStream out = conn.getOutputStream(); out.write("password=xxxxxxxxxxxxxxxxxxxx".getBytes()); conn.getResponseCode(); }

 

3..运用Range字段的 断点续传

public static void main(String[] args) throws Exception { //HttpURLConnection //断点续传 URL url = new URL("http://localhost:8080/md/1.txt"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //拿到Range的请求 conn.addRequestProperty("Range", " bytes=5-"); InputStream in = conn.getInputStream(); int len = 0; byte buffer[] = new byte[1024]; FileOutputStream out = new FileOutputStream("c://1.txt",true); //在bytes=5时追加 while((len=in.read(buffer))>0){ out.write(buffer, 0, len); } in.close(); out.close(); }

 

 

 

 

 

你可能感兴趣的:(exception,浏览器,服务器,url,buffer,byte)