码迷,mamicode.com
首页 > Web开发 > 详细

http协议

时间:2017-11-27 01:15:41      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:connect   url   协议   int   tom   自带   rac   编码格式   get   

http协议: 对浏览器客户端 和  服务器端 之间数据传输的格式规范

查看http协议的工具

1)使用火狐的firebug插件(右键->firebug->网络)

                                     2)使用谷歌的“审查元素”

                                     3)使用系统自带的telnet工具(远程访问工具)                              

                                                        a)telnet localhost 8080      访问tomcat服务器

                                                        b)ctrl+]     回车          可以看到回显

                                                        c)输入请求内容

GET /day09/hello HTTP/1.1
Host: localhost:8080

http协议版本

                   http1.0:当前浏览器客户端与服务器端建立连接之后,只能发送一次请求,一次请求之后连接关闭。

                   http1.1:当前浏览器客户端与服务器端建立连接之后,可以在一次连接中发送多次请求。(基本都使用1.1)

请求资源

                                     URL:  统一资源定位符。http://localhost:8080/day09/testImg.html。只能定位互联网资源。是URI 的子集。

                                     URI: 统一资源标记符。/day09/hello。用于标记任何资源。可以是本地文件系统,局域网的资源(//192.168.14.10/myweb/index.html),                                                   可以是互联网。

请求方式

                            常见的请求方式: GET 、 POST、 HEAD、 TRACE、 PUT、 CONNECT 、DELETE 

 

                            常用的请求方式: GET  和 POST     

 

                            表单提交:

                                     <form action="提交地址" method="GET/POST">

GET   vs  POST 区别

 

                            1)GET方式提交

                                               a)地址栏(URI)会跟上参数数据。以?开头,多个参数之间以&分割。

b)GET提交参数数据有限制,不超过1KB。

                                               c)GET方式不适合提交敏感密码。

                                               d)注意: 浏览器直接访问的请求,默认提交方式是GET方式

                            2)POST方式提交

                                     a)参数不会跟着URI后面。参数而是跟在请求的实体内容中。没有?开头,多个参数之间以&分割。

                                     <form>

 

                            GET   vs  POST 区别

 

                            1)GET方式提交

                                               a)地址栏(URI)会跟上参数数据。以?开头,多个参数之间以&分割。

请求头

Accept: text/html,image/*      -- 浏览器接受的数据类型

Accept-Charset: ISO-8859-1     -- 浏览器接受的编码格式

Accept-Encoding: gzip,compress  --浏览器接受的数据压缩格式

Accept-Language: en-us,zh-       --浏览器接受的语言

Host: www.it315.org:80          --(必须的)当前请求访问的目标地址(主机:端口)

If-Modified-Since: Tue, 11 Jul 2000 18:23:51 GMT  --浏览器最后的缓存时间

Referer: http://www.it315.org/index.jsp      -- 当前请求来自于哪里

User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)  --浏览器类型

Cookie:name=eric                     -- 浏览器保存的cookie信息

Connection: close/Keep-Alive            -- 浏览器跟服务器连接状态。close: 连接关闭  keep-alive:保存连接。

Date: Tue, 11 Jul 2000 18:23:51 GMT      -- 请求发出的时间

实体内容

只有POST提交的参数会放到实体内容中

HttpServletRequest对象

                            HttpServletRequest对象作用是用于获取请求数据。

 

                                        核心的API:

                                               请求行:

                                                        request.getMethod();   请求方式

                                                        request.getRequetURI()   / request.getRequetURL()   请求资源

                                                        request.getProtocol()   请求http协议版本

                                              

                                               请求头:

                                                        request.getHeader("名称")   根据请求头获取请求值

                                                        request.getHeaderNames()    获取所有的请求头名称

 

                                               实体内容:

                                                        request.getInputStream()   获取实体内容数据

/**
 * 请求数据的获取
 * @author APPle
 *
 */
public class RequestDemo1 extends HttpServlet {

    /**
     * 1)tomcat服务器接收到浏览器发送的请求数据,然后封装到HttpServetRequest对象
     * 2)tomcat服务器调用doGet方法,然后把request对象传入到servlet中。
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        /**
         * 3)从request对象取出请求数据。
         */
        //t1(request);
        
        //t2(request); 
    }
    // 为了接收POST方式提交的请求
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse resp)
            throws ServletException, IOException {
        /**
         * 3.3 请求的实体内容
         */
        InputStream in = request.getInputStream(); //得到实体内容
        byte[] buf = new byte[1024];
        int len = 0;
        while(  (len=in.read(buf))!=-1 ){
            String str = new String(buf,0,len);
            System.out.println(str);
        }
    }

    private void t2(HttpServletRequest request) {
        /**
         * 3.2 请求头
         */
        String host = request.getHeader("Host"); //根据头名称的到头的内容
        System.out.println(host);
        
        //遍历所有请求头
        Enumeration<String> enums = request.getHeaderNames(); //得到所有的请求头名称列表
        while(enums.hasMoreElements()){//判断是否有下一个元素
            String headerName = enums.nextElement(); //取出下一个元素
            String headerValue = request.getHeader(headerName);
            System.out.println(headerName+":"+headerValue);
        }
    }

    private void t1(HttpServletRequest request) {
        /**
         * 3.1 请求行   格式:(GET /day09/hello HTTP/1.1)
         */
        System.out.println("请求方式:"+request.getMethod());//请求方式
        System.out.println("URI:"+request.getRequestURI());//请求资源
        System.out.println("URL:"+request.getRequestURL());
        System.out.println("http协议版本:"+request.getProtocol());//http协议
    }

}

 

http协议

标签:connect   url   协议   int   tom   自带   rac   编码格式   get   

原文地址:http://www.cnblogs.com/chenzxl/p/7901193.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!