• <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...

            堅(jiān)信:勤能補(bǔ)拙

            2011知識點(diǎn) - HTTP協(xié)議

            HTTP protocol

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

            HTTP protocol

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

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

            【request】

            一個(gè)request通常包含如下內(nèi)容:

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

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

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

            一般HTTP Server需要實(shí)現(xiàn)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之后會斷開連接,如設(shè)置為Keep-Alive,則該連接可以多次使用
                Cookie: username=my-username; password=my-password; //指定client端攜帶的cookies
                Content-Length: 348         //數(shù)據(jù)包內(nèi)容的長度(之后message body部分)
                Content-Type: application/x-www-form-urlencoded //數(shù)據(jù)報(bào)內(nèi)容的類型
            

            【response】

            一個(gè)response通常包含如下內(nèi)容:

                response line
                header fields (key-value pairs)
                empty line
                message body
            
            • 每行必須使用\r\n結(jié)尾;
            • reponse line的部分包含status code和status reason phrase,如HTTP/1.1 200 OKHTTP/1.1 404 Not Found
            • 空行部分不能包含任何空格字符;
            • message body部分即獲取的URI的資源的內(nèi)容,如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部分內(nèi)容長度
                Content-Type:  text/html;charset=gb2312     //reponse body部分的類型和編碼
                Cache-Control: max-age=3600                 //指定client可以cache
                Content-Encoding: gzip                      //指定response內(nèi)容是壓縮的
                Set-Cookie: userId=2; username=min;         //在client端種下cookies
            

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

            【HTTP request和response實(shí)例】

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

            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是無狀態(tài)的協(xié)議,server端不保留client端的任何狀態(tài)信息,因此要實(shí)現(xiàn)在多次連接下能夠獲取之前的狀態(tài),可以通過cookies(client端)或session(server端)的方法。

            【HTTP協(xié)議的應(yīng)用】

            綜上,HTTP協(xié)議即是WWW互聯(lián)網(wǎng)基于TCP socket定義的一組通訊規(guī)范,使用該協(xié)議即可實(shí)現(xiàn)http client和http server的通信。它的優(yōu)點(diǎn)就是簡單,最常用,大部分語言都支持client端實(shí)現(xiàn)

            實(shí)際中借助libevent庫可輕松實(shí)現(xiàn)高并發(fā)高性能的HTTP server,將自己的應(yīng)用封裝一個(gè)HTTP接口,方便各種客戶端進(jìn)行數(shù)據(jù)通信。

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

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

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

            導(dǎo)航

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

            統(tǒng)計(jì)

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久精品国产69国产精品亚洲| 国产精品永久久久久久久久久| 中文字幕日本人妻久久久免费 | 国产午夜福利精品久久| 久久久WWW成人| 久久热这里只有精品在线观看| 人妻精品久久无码专区精东影业| 国产麻豆精品久久一二三| 国产精品99久久不卡| 久久久久久精品成人免费图片| 国产精品久久99| 久久久久久国产a免费观看黄色大片| 亚洲国产精品久久久天堂| 久久精品这里只有精99品| AV无码久久久久不卡网站下载 | 久久亚洲AV成人出白浆无码国产| 久久综合综合久久97色| 亚洲国产欧美国产综合久久 | 无码精品久久久天天影视| 久久久久无码精品国产app| 97精品久久天干天天天按摩| 久久精品日日躁夜夜躁欧美| 久久久久久A亚洲欧洲AV冫 | 94久久国产乱子伦精品免费| 无码精品久久久久久人妻中字| 欧美久久一级内射wwwwww.| 国产精品亚洲美女久久久| 亚洲国产二区三区久久| A狠狠久久蜜臀婷色中文网| 一本色道久久综合狠狠躁| 久久久久99这里有精品10| 欧美精品福利视频一区二区三区久久久精品 | 久久精品国产只有精品66| 精品久久久无码人妻中文字幕豆芽| 亚洲国产成人久久笫一页 | 精品国产乱码久久久久软件| 久久久久亚洲爆乳少妇无| 国产成人香蕉久久久久| 精品国产91久久久久久久| 久久久无码精品亚洲日韩蜜臀浪潮| 久久大香萑太香蕉av|