Http(1)

來源:互聯網
上載者:User

標籤:post   script   title   分享   err   extend   out   多次   agent   

#http協議版本

                   http1.0:當前瀏覽器用戶端與伺服器端建立串連之後,只能發送一次請求,一次請求之後串連關閉。

                   http1.1:當前瀏覽器用戶端與伺服器端建立串連之後,可以在一次串連中發送多次請求。(基本都使用1.1)

#請求資源

                  URL:  統一資源定位器。http://localhost:8080/day11/testImg.html。只能定位互連網資源。是URI的子集。

                  URI: 統一資源標記符。/day11/hello。用於標記任何資源。可以是本地檔案系統,區域網路的資(//192.168.14.10/myweb/index.html),可以是互連網。

#請求方式

                            常見的請求方式: GET 、 POST、 HEAD、 TRACE、 PUT、 CONNECT 、DELETE  

 

                            常用的請求方式: GET(瀏覽器預設的請求方式)  和 POST      

#要求標頭

        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請求才有實體。(post請求一般用於提交比較交敏感的資料)

HttpServletRequest對象

      HttpServletRequest對象作用是用於擷取請求資料        

    核心的API:

 

                請求行:

 

                                                        request.getMethod();   請求方式

 

                                                        request.getRequetURI()   / request.getRequetURL()   請求資源

 

                                                        request.getProtocol()   請求http協議版本

 

                                              

 

                                               要求標頭:

 

                                                        request.getHeader("名稱")   根據要求標頭擷取請求值

 

                                                        request.getHeaderNames()    擷取所有的要求標頭名稱

 

 

 

                                               實體內容:

 

                                                        request.getInputStream()   擷取實體內容資料

範例程式碼

1、jsp代碼

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP ‘index.jsp‘ starting page</title>    <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="This is my page">    <!--    <link rel="stylesheet" type="text/css" href="styles.css">    -->  </head>    <body>    <form action="/HttpTest/HttpTest" method="get">        使用者名稱:<input type="text" name="name"></br>        密  碼  :<input type="text" name="password"></br>        <input type="submit" value="提交">    </form>    </hr>    </br>    </br>    <form action="/HttpTest/HttpTest" method="POST">        使用者名稱:<input type="text" name="name"></br>        密  碼  :<input type="text" name="password"></br>        <input type="submit" value="提交">    </form>  </body></html>

2、servlet代碼

public class HttpTest extends HttpServlet {    private static final long serialVersionUID = 1L;    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        //t1(request);            //    t2(request);    }    //接收post方式的請求    @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp)            throws ServletException, IOException {        InputStream in=req.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) {        /**         * 要求標頭         */        Enumeration<String> enums=request.getHeaderNames();        while (enums.hasMoreElements()) {            String string = (String) enums.nextElement();            String value=request.getHeader(string);            System.out.println(string+" : "+value);        }    }    private void t1(HttpServletRequest request) {        /**         * 請求行         */        System.out.println("請求的方式:"+request.getMethod());        System.out.println("URI:"+request.getRequestURI());        System.out.println("URL:"+request.getRequestURL());        System.out.println("http:"+request.getProtocol());    }}

 

Http(1)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.