2015-07-20 38 views
0

我是Client Server應用程序的初學者。在閱讀了幾個論壇之後,我開發了一個帶Java Server的基本Javascript客戶端,並嘗試使用POST發送數據。但是,當我成功建立它們之間的連接時,在服務器端打印接收到的數據時,它僅打印標題而不打印實際的數據內容。像這樣AJAX:POST方法內容不傳輸

The Client /127.0.0.1:34290 is connected 
The HTTP request string is .... 
POST/HTTP/1.1 
Host: 127.0.0.1:8080 
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0 
Accept: */* 
Accept-Language: en-US,en;q=0.5 
Accept-Encoding: gzip, deflate 
Content-Type: application/x-www-form-urlencoded; charset=UTF-8 
Content-Length: 30 
Origin: null 
Connection: keep-alive 
Pragma: no-cache 
Cache-Control: no-cache 

客戶端代碼:

$(document).ready(function(){ 
    $("button").click(function(){ 
     $.post("demo_test_post.asp", 
     { 
      name: "Donald Duck", 
      city: "Duckburg" 
     }, 
     function(data,status){ 
      alert("Data: " + data + "\nStatus: " + status); 
     }); 
    }); 
}); 

服務器端代碼:

inFromClient = new BufferedReader(new InputStreamReader(connectedClient.getInputStream())); 
outToClient = new DataOutputStream(connectedClient.getOutputStream()); 

String requestString = inFromClient.readLine(); 
String headerLine = requestString; 

StringBuffer responseBuffer = new StringBuffer(); 
responseBuffer.append("<b> This is the HTTP Server Home Page.... </b><BR>"); 
responseBuffer.append("The HTTP Client request is ....<BR>"); 

System.out.println("The HTTP request string is ...."); 
while (inFromClient.ready()) { 
// Read the HTTP complete HTTP Query 
    responseBuffer.append(requestString + "<BR>"); 
    System.out.println(requestString); 
    requestString = inFromClient.readLine(); 
} 

可有人告訴我,我錯了?

回答

0

看起來您的服務器代碼不會打印請求中的最後一行(它通過inFromClient.readLine()讀取它,然後inFromClient.ready()切換爲false,最後一行在未打印的情況下被遺忘)。

試試這個(沒有響應的東西,只是爲了驗證請求完成):

inFromClient = new BufferedReader(new InputStreamReader(connectedClient.getInputStream())); 

String requestString; 
while ((requestString = inFromClient.readLine()) != null) { 
    System.out.println(requestString); 
} 
+0

呀已經嘗試過。輸出是這個'Host:127.0.0.1:8080 Accept:*/* Accept-Encoding:gzip,deflate Content-Length:30 Connection:keep-alive Cache-Control:no-cache' – Enigma