2015-12-19 90 views
0

我想從Java中讀取POST數據。 我有形式registrationForm.html:Http協議POST閱讀

<form action="registratrationResult.html" method="post"> 
    Mail:<br> 
    <input type="text" name="mail"> 
    <br> 
    Password:<br> 
    <input type="password" name="password"> 
    <input type="submit"> 
</form> 

我想在java中不使用servlet來閱讀。我讀到的一切,從瀏覽器中,我得到了它:

>POST /registratrationResult.html HTTP/1.1 
>Host: localhost:8080 
>Connection: keep-alive 
>Content-Length: 29 
>Cache-Control: max-age=0 
>Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 
>Origin: http://localhost:8080 
>Upgrade-Insecure-Requests: 1 
>User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36 
>Content-Type: application/x-www-form-urlencoded 
>Referer: http://localhost:8080/registrationForm.html 
>Accept-Encoding: gzip, deflate 
>Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4 

我看,應該是這樣的:

parameter=value&also=another 

如何獲得呢? 當我更改輸入數據時,內容長度正在改變。

+0

我想看看如何獲​​得POST響應的代碼示例,它是在服務器端還是客戶端? –

回答

0

正如您所說,實際的POST數據存儲爲鍵值對,但它們放在請求正文中,而不是標題。

樣品使用捲曲:

$ curl --data "fname=stackoverflow&lname=user" --trace - http://www.w3schools.com/tags/demo_form_method_post.asp 

注意輸出發送數據區域:

=> Send data, 30 bytes (0x1e) 
0000: 66 6e 61 6d 65 3d 73 74 61 63 6b 6f 76 65 72 66 fname=stackoverf 
0010: 6c 6f 77 26 6c 6e 61 6d 65 3d 75 73 65 72  low&lname=user 
== Info: upload completely sent off: 30 out of 30 bytes 

POST請求通常會與內容類型的application/x-www-form-urlencoded發送,如提及發送標題。

+0

是否有可能在Java中讀取它? 我如何閱讀請求正文? –