1. 程式人生 > >[web] request的Content-Type小結

[web] request的Content-Type小結

本文轉載自多篇文章,在這裡記錄一下。

request的Content-Type小結

application/x- www-form-urlencoded是Post請求預設的請求體內容型別,也是form表單預設的型別。Servlet API規範中對該型別的請求內容提供了request.getParameter()方法來獲取請求引數值。但當請求內容不是該型別時,需要呼叫request.getInputStream()或request.getReader()方法來獲取請求內容值。

當請求體內容(注意:get請求沒有請求體)型別是application/x- www-form-urlencoded時也可以直接呼叫request.getInputStream()或request.getReader()方法獲取到請求內容再解析出具體都引數,但前提是還沒呼叫request.getParameter()方法。此時當request.getInputStream()或request.getReader()獲取到請求內容後,無法再調request.getParameter()獲取請求內容。即對該型別的請求,三個方法互斥,只能調其中一個。今天遇到一個Controller請求經過Spring MVC 的RequestMapping處理後,只能通過request.getParameter()獲取到引數、無法通過request.getInputStream()和request.getReader()讀取內容很可能就是因為在請求經過Spring MVC時已呼叫過request.getParameter()方法的原因。

注意:在一個請求鏈中,請求物件被前面物件方法中呼叫request.getInputStream()或request.getReader()獲取過內容後,後面的物件方法裡再呼叫這兩個方法也無法獲取到客戶端請求的內容,但是呼叫request.getParameter()方法獲取過內容後,後面的物件方法裡依然可以呼叫它獲取到引數的內容。

當請求體內容是其它型別時,比如 multipart/form-data或application/json時,無法通過request.getParameter()獲取到請求內容,此時只能通過request.getInputStream()和request.getReader()方法獲取請求內容,此時呼叫request.getParameter()也不會影響第一次呼叫request.getInputStream()或request.getReader()獲取到請求內容。

request.getInputStream()返回請求內容位元組流,多用於檔案上傳,request.getReader()是對前者返回內容的封裝,可以讓呼叫者更方便字元內容的處理(不用自己先獲取位元組流再做字元流的轉換操作)。

=========================================

最近做專案時,發現手機客戶端通過http協議post方式上傳資料到服務端,在伺服器端通過request.getInputStream()能獲取到相應的資料,但用request.getParameter()卻獲取不到資料。這是怎麼回事呢,後來發現這種情況跟form表單的屬性 enctype有關係。 
HTML中的form表單有一個關鍵屬性 enctype=application/x-www-form-urlencoded 或multipart/form-data。

1、enctype=”application/x-www-form-urlencoded”是預設的編碼方式,當以這種方式提交資料時,HTTP報文中的內容是: 
Html程式碼 收藏程式碼 
POST /post_test.php HTTP/1.1 
Accept-Language: zh-CN 
User-Agent: Mozilla/4.0 
Content-Type: application/x-www-form-urlencoded 
Host: 192.168.12.102 
Content-Length: 42 
Connection: Keep-Alive 
Cache-Control: no-cache

title=test&content=%B3%AC%BC%B6%C5%AE%C9%FA&submit=post+article 

Servlet的API提供了對這種編碼方式解碼的支援,只需要呼叫ServletRequest 類中的getParameter()方法就可以得到表單中提交的資料。

2、在傳輸大資料量的二進位制資料時,必須將編碼方式設定成enctype=”multipart/form-data”,當以這種方式提交資料時,HTTP報文中的內容是:

Html程式碼 收藏程式碼 
POST /post_test.php?t=1 HTTP/1.1 
Accept-Language: zh-CN 
User-Agent: Mozilla/4.0 
Content-Type: multipart/form-data; boundary=—————————7dbf514701e8 
Accept-Encoding: gzip, deflate 
Host: 192.168.12.102 
Content-Length: 345 
Connection: Keep-Alive 
Cache-Control: no-cache

—————————–7dbf514701e8 
Content-Disposition: form-data; name=”title” 
test 
—————————–7dbf514701e8 
Content-Disposition: form-data; name=”content” 
…. 
—————————–7dbf514701e8 
Content-Disposition: form-data; name=”submit” 
post article 
—————————–7dbf514701e8– 
如果以這種方式提交資料就要用request.getInputStream()或request.getReader()來獲取提交的資料 ,用 request.getParameter()是獲取不到提交的資料的。

最後注意request.getParameter()、request.getInputStream()、request.getReader()這三種方法是有衝突的,因為流只能被讀一次。 
比如: 
當form表單內容採用enctype=application/x-www-form-urlencoded編碼時,先通過呼叫request.getParameter()方法獲取資料後,再呼叫request.getInputStream()或request.getReader()已經獲取不到流中的內容了,因為在呼叫 request.getParameter()時系統可能對錶單中提交的資料以流的形式讀了一次,反之亦然。

當form表單內容採用enctype=multipart/form-data編碼時,呼叫request.getParameter()獲取不到資料,即使已經呼叫了request.getParameter()方法也可以再通過呼叫request.getInputStream()或request.getReader()獲取表單中的資料,但request.getInputStream()和request.getReader()在同一個響應中是不能混合使用的,如果混合使用會拋異常的。

 

============================

 最近專案業務涉及到服務端(Java)與服務端(c++)通訊,我是屬於java端,c++端通過拼接http請求頭向java端傳送資料。發現數據可以接收到,但是資料裡如果出現+號會將+號轉義為空格。經排查發現是因為請求頭的Content-Type屬性為application/x-www-form-urlencoded,這樣會導致url加密,從而將特殊字元轉義使得收到的引數不夠準確。所以Content-Type 就不能再使用application/x-www-form-urlencoded型別。應改為text/html或text/plain來避免將特殊字串轉義。但是如此一來發送的引數就不是form鍵值對的形式了。java端無法用傳統的接收方法來接收。這個時候就要直接從request的輸入流中去取引數。程式碼如下:

//獲取post引數 
StringBuffer sb = new StringBuffer() ; 
InputStream is = request.getInputStream(); 
InputStreamReader isr = new InputStreamReader(is);   
BufferedReader br = new BufferedReader(isr); 
String s = "" ; 
while((s=br.readLine())!=null){ 
sb.append(s) ; 

String str =sb.toString();
--------------------- 
作者:當時年少春衫薄丶 
來源:CSDN 
原文:https://blog.csdn.net/cxfly957/article/details/78785498?utm_source=copy 
版權宣告:本文為博主原創文章,轉載請附上博文連結!