HTTP Request Method

HTTP/1.1協議中共定義了八種方法來代表Request-URI指定的資源的不一樣操做方式:
方法名稱區分大小寫html

  • OPTIONS 用於請求得到由Request-URI標識的資源在請求/響應的通訊過程當中可使用的功能選項(Access-Control-Allow-*), 也能夠利用向Web服務器發送'*'的請求來測試服務器的功能性。
    若是沒有響應正文,響應必須包含Content-Length,而且值爲「0」
  • HEAD   向服務器索要與GET請求相一致的響應,只不過響應體將不會被返回。這一方法能夠在不用傳輸整個響應內容的狀況下,就能夠獲取包含在響應消息頭中的元信息。
  • GET      向特定的資源發出請求。
  • POST   向指定資源提交數據進行處理請求(例如提交表單或者上傳文件)。
  • PUT     向指定資源位置上傳其最新內容。
  • DELETE 請求服務器刪除Request-URI所標識的資源。
  • TRACE   回顯服務器收到的請求,主要用於測試或診斷。
  • CONNECT  HTTP/1.1協議中預留給可以將鏈接改成管道方式的代理服務器。

ps.
當某個請求所針對的資源不支持對應的請求方法的時候,服務器應當返回狀態碼405(Method Not Allowed);
當服務器不認識或者不支持對應的請求方法的時候,應當返回狀態碼501(Not Implemented)。java

GET與POST區別

  • GET:  安全性相對低
    Query String Parameters ( Query String) 的方式, 將參數拼接在URL中,  所以GET可提交的數據量就跟URL所能達到的最大長度有直接關係(HTTP協議規範也沒有對URL長度進行限制,這個限制是特定的瀏覽器及服務器對它的限制。 eg.  IE對URL長度的限制是2083字節(2K+35字節))。若是url太長,服務器可能會由於安全方面的設置從而拒絕請求或者發生不完整的數據請求。
  • POST: 安全性相對高 
    Request Payload (Request Body)提交; 理論上講是沒有數據大小限制, HTTP協議規範也沒有進行限制,但實際上post所能傳遞的數據量大小取決於服務器的設置和內存大小。

GET產生一個TCP數據包;POST產生兩個TCP數據包!web

GET:瀏覽器會把http header和data一併發送出去,服務器響應200(返回數據);
POST: 瀏覽器先發送header,服務器響應100 continue,瀏覽器再發送data,服務器響應200 ok(返回數據)。
spring

中文亂碼:
      Post方式:在JSP中request解析數據時設置編碼格式:request.setCharacterEncoding("utf-8"); 也可使用Spring的CharacterEncodingFilter統一setCharacterEncoding。windows

<!-- characterEncodingFilter字符編碼過濾器 -->
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <!--要使用的字符集,通常咱們使用UTF-8(保險起見UTF-8最好)-->
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <!--是否強制設置request的編碼爲encoding,默認false,不建議更改-->
            <param-name>forceRequestEncoding</param-name>
            <param-value>false</param-value>
        </init-param>
        <init-param>
            <!--是否強制設置response的編碼爲encoding,建議設置爲true-->
            <param-name>forceResponseEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <!--這裏不能留空或者直接寫 ' / ' ,否者不起做用-->
        <url-pattern>/*</url-pattern>
    </filter-mapping>
CharacterEncodingFilter:

private String encoding; //要使用的字符集,通常咱們使用UTF-8(保險起見UTF-8最好)
private boolean forceRequestEncoding = false; //是否強制設置request的編碼爲encoding
private boolean forceResponseEncoding = false; //是否強制設置response的編碼爲encoding

 @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {

        String encoding = getEncoding();
        if (encoding != null) { //若是設置了encoding的值,則根據狀況設置request和response的編碼
            //若設置request強制編碼或request自己就沒有設置編碼則設置編碼爲encoding表示的值
            if (isForceRequestEncoding() || request.getCharacterEncoding() == null) { 
                request.setCharacterEncoding(encoding);
            }
            //若設置response強制編碼,則設置編碼爲encoding表示的值
            if (isForceResponseEncoding()) { 
                response.setCharacterEncoding(encoding);
            }
        }
        filterChain.doFilter(request, response);
    }

Get方式:  對url編碼encodeURI; 修改tomcat的配置server.xml   <Connector> URIEncoding="UTF-8"。瀏覽器

<!-- windows  -->
<Service name="Catalina">
    <Connector port="8080" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" URIEncoding="UTF-8" />
 
    <Connector port="8099" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8" />

 

引貼: GET和POST兩種基本請求方法的區別tomcat

相關文章
相關標籤/搜索