HTTP協議中的GET和POST

來源:互聯網
上載者:User

HTTP協議中GET方法和POST方法是有所不同的,RFC1945中的英文原文如下:

8.1 GET

The GET method means retrieve whatever information (in the form of an
entity) is identified by the Request-URI. If the Request-URI refers
to a data-producing process, it is the produced data which shall be
returned as the entity in the response and not the source text of the
process, unless that text happens to be the output of the process.

The semantics of the GET method changes to a "conditional GET" if the
request message includes an If-Modified-Since header field. A
conditional GET method requests that the identified resource be
transferred only if it has been modified since the date given by the
If-Modified-Since header, as described in Section 10.9. The
conditional GET method is intended to reduce network usage by
allowing cached entities to be refreshed without requiring multiple
requests or transferring unnecessary data.

8.3 POST

The POST method is used to request that the destination server accept
the entity enclosed in the request as a new subordinate of the
resource identified by the Request-URI in the Request-Line. POST is
designed to allow a uniform method to cover the following functions:

     o Annotation of existing resources;

     o Posting a message to a bulletin board, newsgroup, mailing list,
or similar group of articles;

     o Providing a block of data, such as the result of submitting a
form [3], to a data-handling process;

     o Extending a database through an append operation.

The actual function performed by the POST method is determined by the
server and is usually dependent on the Request-URI. The posted entity
is subordinate to that URI in the same way that a file is subordinate
to a directory containing it, a news article is subordinate to a
newsgroup to which it is posted, or a record is subordinate to a
database.

A successful POST does not require that the entity be created as a
resource on the origin server or made accessible for future
reference. That is, the action performed by the POST method might not
result in a resource that can be identified by a URI. In this case,
either 200 (ok) or 204 (no content) is the appropriate response
status, depending on whether or not the response includes an entity
that describes the result.

If a resource has been created on the origin server, the response
should be 201 (created) and contain an entity (preferably of type
"text/html") which describes the status of the request and refers to
the new resource.

A valid Content-Length is required on all HTTP/1.0 POST requests. An
HTTP/1.0 server should respond with a 400 (bad request) message if it
cannot determine the length of the request message's content.

Applications must not cache responses to a POST request because the
application has no way of knowing that the server would return an
equivalent response on some future request.

 

翻譯成中文(黃曉東翻譯,xdhuang@eyou.com)就是

8.1 GET

GET方法就是以實體方式得到由請求URI所指定資源的資訊。如果請求URI只是一
個資料產生過程,那麼最終要在回應實體中返回的是由該處理過程的結果所指向
的資源,而不是返回該處理過程的標題文字,除非那段文字恰好是處理的輸出。
如果請求訊息包含If-Modified-Since標題域,GET方法的文法就變成“條件GET”,
即“(conditional GET)”。 條件GET方法可以對指定資源進行判斷,如果它在
If-Modified-Since標題域(見10.9節)中的指定日期後發生了更新,才啟動傳
輸,否則不傳輸。這種條件GET允許被緩衝的實體在不必經過多次請求或不必要
的資料轉送就能進行重新整理,從而有助於降低網路負載。

8.3 POST

POST方法用來向目的伺服器發出請求,要求它接受被附在請求後的實體,並把它
當作請求隊列(Request-Line)中請求URI所指定資源的附加新子項。POST被設計
成用統一的方法實現下列功能:

     o 對現有資源的注釋(Annotation of existing resources);

     o 向電子公告欄、新聞群組,郵件清單或類似討論群組發送訊息;

     o 提交資料區塊,如將表格(form [3])的結果提交給資料處理過程;

     o 通過附加操作來擴充資料庫。

POST方法的實際功能由伺服器來決定,而且通常依賴於請求URI。在POST過程中,
實體是URI的從屬部分,就好象檔案從屬於包含它的目錄、新聞群組檔案從屬於發出
該檔案的新聞群組、記錄從屬於其所在的資料庫一樣。

成功的POST不需要在原始伺服器建立實體,並將其做為資源;也不需要為未來的
訪問提供條件。也就是說,POST方法不一定會指向URI指定的資源。在這種情況下,
200(成功)或204(無內容)都是適當的回應狀態,取決於實際回應實體中對結
果的描述。

如果在原始伺服器上建立了資源,回應應是201(已建立),並包含一個實體
(對"text/html"類型最為適合),該實體中記錄著對新資源請求的狀態原因。

在所有的HTTP/1.0的POST請求中,必須指定合法的內容長度(Content-Length)。
如果HTTP/1.0伺服器在接收到請求訊息內容時無法確定其長度,就會返回400(非
法請求)代碼。

應用程式不能緩衝對POST請求的回應,因為做為應用程式來說,它們沒有辦法知道
伺服器在未來的請求中將如何回應。

總結和解釋:

一、post和get方法在使用上至少有兩點不同:

  1. GET方法通過URL請求來傳遞使用者的輸入。即把參數資料隊列加到提交表單的ACTION屬性所指的URL中,值和表單內各個欄位一一對應,在URL中可以看到。POST是通過HTTP POST機制,將表單內各個欄位與其內容放置在HTML HEADER內一起傳送到ACTION屬性所指的URL地址,使用者看不到這個過程。
  2. GET方式的提交你需要用Request.QueryString來取得變數的值,而POST方式提交時,你必須通過Request.Form來訪問提交的內容。

提示

通過GET方法提交資料,可能會帶來安全性的問題。比如一個登陸頁面。當通過GET方法提交資料時,使用者名稱和密碼將出現在URL上。如果:

    • 登陸頁面可以被瀏覽器緩衝;
    • 其他人可以訪問客戶的這台機器。

那麼,別人即可以從瀏覽器的記錄中,讀取到此客戶的帳號和密碼。所以,在某些情況下,get方法會帶來嚴重的安全性問題。

建議 :在Form中,建議使用POST方法。

二、<form action="" method="POST">重新整理時會彈出提示框,問是否重新發送請求,若改為method="GET"則不會有該提示;且POST請求無長度限制(至少是理論上的),GET有長度限制,最長不超過2048位元組。

 

註:“總結和解釋”部分的內容從網上取得。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.