• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            A Za, A Za, Fighting...

            堅信:勤能補拙

            2011知識點 - HTTP協議

            HTTP protocol

            © Min的技術分享 – 54min.com (RSS訂閱) | 原文鏈接:http://54min.com/post/the-http-protocol.html

            HTTP protocol

            HTTP是應用層協議(傳輸層采用TCP因此是面向連接的),是WWW所使用的協議。當前使用的是HTTP/1.1(HTTP/1.0對每個request/response使用獨立的connection,每次請求都需要重新建立到server的HTTP連接;HTTP/1.1引入了HTTP persistent connection,重用一個connection多次(通過在client和server的HTTP header中添加Connection: Keep-Alive),參考)。

            HTTP-base的C/S通信: client(常見的為瀏覽器)發出request, server(常見的為http server)返回針對該request的response。(通常采用80端口)。

            【request】

            一個request通常包含如下內容:

                request line
                header fields (key-value pairs)
                empty line
                optional message body
            
            • 每行必須使用\r\n結尾;
            • request line的部分包含請求文件的路徑部分,例如GET /index.html HTTP/1.1
            • request header fields部分只有Host是不可省略的,其他都是可選的;
            • 空行部分不能包含任何空格字符;
            • message body部分通常用于放置POST方法的數據,是可選的

            request支持的方法由如下幾種(request line的部分指定):

                HEAD    //和GET方法類似,但是只獲取response的HTTP header
                GET     //獲取指定路徑的內容(reponse包含header和message body)
                POST    //將數據POST到server
                PUT     //uploads a representation of the specified resource
                DELETE  //delete the specified resource
            
                TRACE   //返回server得到的request,以了解在中間server進行了哪些修改
                OPTIONS
                CONNECT
                PATCH
            

            一般HTTP Server需要實現GET,HEAD和OPTIONS方法。

            request的HTTP header fields參考。常見的:

                Host: www.google.com        //server的domain name,不能省略
                Accpet: text/plain          //接受的Content-Type
                Accept-Charset: utf-8       //接受的character set
                Accept-Language: en-US      //接受的語言
                User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
                Connection: Close           //客戶端在獲取response之后會斷開連接,如設置為Keep-Alive,則該連接可以多次使用
                Cookie: username=my-username; password=my-password; //指定client端攜帶的cookies
                Content-Length: 348         //數據包內容的長度(之后message body部分)
                Content-Type: application/x-www-form-urlencoded //數據報內容的類型
            

            【response】

            一個response通常包含如下內容:

                response line
                header fields (key-value pairs)
                empty line
                message body
            
            • 每行必須使用\r\n結尾;
            • reponse line的部分包含status code和status reason phrase,如HTTP/1.1 200 OKHTTP/1.1 404 Not Found
            • 空行部分不能包含任何空格字符;
            • message body部分即獲取的URI的資源的內容,如HTML頁面代碼

            response的status code,比較常見到的:

            200 OK
            
            302 Found   //用于redirection
            
            400 Bad Request //the request has bad syntax
            403 Forbidden   //the server is refusing to respond to it
            404 Not Found   //could not be found
            
            502 Bad Gateway //the server was acting as proxy and received an invalid response from the upstream server(一般為app server),如nginx + PHP-FPM
            

            更多參考:http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

            response的HTTP header fields參考。常見的:

                Server: nginx/0.7.67                        //http server信息
                Connection: close                           //或Keep-Alive
                Content-Length: 35                          //response body部分內容長度
                Content-Type:  text/html;charset=gb2312     //reponse body部分的類型和編碼
                Cache-Control: max-age=3600                 //指定client可以cache
                Content-Encoding: gzip                      //指定response內容是壓縮的
                Set-Cookie: userId=2; username=min;         //在client端種下cookies
            

            更多的Http headers fields: http://en.wikipedia.org/wiki/List_of_HTTP_header_fields

            【HTTP request和response實例】

            如下介紹HEADGETPOST共3種方法的HTTP request/response實例。

            HEAD:

            HEADGET類似但是只獲取response的response line和header fields部分,不包括message body

            • request:

              HEAD /request.php?q=keywords&status=2 HTTP/1.1
              HOST: example.com
              User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
              
            • response

              HTTP/1.1 200 OK
              Server  nginx/0.7.67
              Date    Mon, 18 Jul 2010 02:51:09 GMT
              Content-Type    text/html
              Transfer-Encoding   chunked
              Connection  keep-alive
              X-Powered-By    PHP/5.3.6-12
              Content-Encoding    gzip
              

            GET:

            • request:

              GET /index.php?q=keywords HTTP/1.1
              Host    test.54min.com
              User-Agent  Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0
              Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
              Connection  keep-alive
              
            • response:

              HTTP/1.1 200 OK
              Server  nginx/0.7.67
              Date    Mon, 18 Jul 2010 02:54:02 GMT
              Content-Type    text/html
              Last-Modified   Fri, 24 Jun 2010 07:39:28 GMT
              Transfer-Encoding   chunked
              Connection  keep-alive
              Content-Encoding    gzip
              
              <html>
              this is c 
              sfsdfsdfs
              </html>
              

            POST:

            POST方法的request需要指定Content-LengthContent-Type

            • request:

              POST /login.php HTTP/1.1
              Host: www.example.com
              User-Agent  Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0
              Content-Length: 27
              Content-Type: application/x-www-form-urlencoded
              
              userid=joe&password=guessme
              
            • response:

              HTTP/1.1 200 OK
              Server  nginx/0.7.67
              Date    Mon, 18 Jul 2010 02:54:02 GMT
              Content-Type    text/html
              Last-Modified   Fri, 24 Jun 2010 07:39:28 GMT
              Transfer-Encoding   chunked
              Connection  keep-alive
              Content-Encoding    gzip
              
              <html>
              this is c 
              sfsdfsdfs
              </html>
              

            參考:http://developers.sun.com/mobility/midp/ttips/HTTPPost/

            【HTTP是statless protocol】

            HTTP是無狀態的協議,server端不保留client端的任何狀態信息,因此要實現在多次連接下能夠獲取之前的狀態,可以通過cookies(client端)或session(server端)的方法。

            【HTTP協議的應用】

            綜上,HTTP協議即是WWW互聯網基于TCP socket定義的一組通訊規范,使用該協議即可實現http client和http server的通信。它的優點就是簡單,最常用,大部分語言都支持client端實現

            實際中借助libevent庫可輕松實現高并發高性能的HTTP server,將自己的應用封裝一個HTTP接口,方便各種客戶端進行數據通信。

            【推薦的HTTP(header和raw message)分析工具】

            參考:http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol

            posted on 2011-09-04 22:19 simplyzhao 閱讀(413) 評論(0)  編輯 收藏 引用 所屬分類: R_找工復習2011

            導航

            <2011年9月>
            28293031123
            45678910
            11121314151617
            18192021222324
            2526272829301
            2345678

            統計

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久综合综合久久综合| 久久亚洲高清综合| 777米奇久久最新地址| 狠狠色丁香婷婷综合久久来 | 欧美成人免费观看久久| 色妞色综合久久夜夜| 久久精品国产精品亚洲精品 | A级毛片无码久久精品免费| 久久国产欧美日韩精品免费| 久久精品国产日本波多野结衣 | 国产情侣久久久久aⅴ免费| 久久精品成人欧美大片| 久久精品中文闷骚内射| 中文字幕久久亚洲一区| 国产综合精品久久亚洲| 日韩精品久久久久久| 欧美午夜精品久久久久免费视| 国产高潮久久免费观看| 国产精品欧美久久久天天影视 | 国产成人精品综合久久久久| 久久99精品国产麻豆不卡| 国产产无码乱码精品久久鸭| 久久精品卫校国产小美女| 久久无码AV中文出轨人妻| 亚洲国产精品一区二区久久| 97热久久免费频精品99| 久久er99热精品一区二区| 综合网日日天干夜夜久久| 久久无码AV中文出轨人妻| 四虎影视久久久免费| 无码人妻久久一区二区三区蜜桃| 久久精品国产秦先生| 国产精品美女久久久久网| 久久九九青青国产精品| 久久99热国产这有精品| 久久精品视频网| 一本伊大人香蕉久久网手机| 一本久久久久久久| 久久国产香蕉一区精品| 三级片免费观看久久| 久久国产精品无|