• <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 閱讀(404) 評論(0)  編輯 收藏 引用 所屬分類: R_找工復習2011

            導航

            <2010年7月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            統計

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久久久久人妻无码| 久久精品无码专区免费 | 精品久久久久国产免费| 久久精品国产一区二区| 久久精品国产亚洲AV影院| 久久福利青草精品资源站免费| 国产综合免费精品久久久| 亚洲中文字幕久久精品无码喷水| 香港aa三级久久三级| 97精品依人久久久大香线蕉97| 国产99久久九九精品无码| 国产成人久久精品一区二区三区| 99久久久久| 99久久99久久精品免费看蜜桃| 污污内射久久一区二区欧美日韩| a级成人毛片久久| 亚洲狠狠婷婷综合久久久久| 久久五月精品中文字幕| 亚洲精品高清国产一久久| 国产美女亚洲精品久久久综合| 久久国产精品二国产精品| 香港aa三级久久三级| 丰满少妇人妻久久久久久| 亚洲综合伊人久久综合| 色综合合久久天天给综看| 狠狠综合久久综合中文88| 国产精品久久精品| 久久人人爽人人爽人人片AV不| 亚洲va久久久久| 久久这里只精品99re66| 久久综合精品国产一区二区三区 | 久久精品?ⅴ无码中文字幕| 69SEX久久精品国产麻豆| 久久精品人成免费| 日本强好片久久久久久AAA| 久久精品国产亚洲AV久| 久久久亚洲AV波多野结衣| 久久婷婷国产剧情内射白浆| 一级女性全黄久久生活片免费 | 麻豆久久久9性大片| 中文字幕精品久久|