jquery ajax和java服务器,丢失数据

我有这个看起来像这样的ajaxfunction

$.ajax({ type: "POST", url: "http://localhost:55556", data: "lots and lots of pie", cache: false, success: function(result) { alert("sent"); }, failure: function() { alert('An Error has occured, please try again.'); } }); 

和一个看起来像这样的服务器

  clientSocket = AcceptConnection(); inp = new BufferedReader(new InputStreamReader (clientSocket.getInputStream())); String requestString = inp.readLine(); BufferedReader ed = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); while(true){ String tmp = inp.readLine(); System.out.println(tmp); } 

现在奇怪的是当我发送我的ajax我的服务器通过使用system.out得到

 Host: localhost:55556 User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:27.0) Gecko/20100101 Firefox/27.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: 20 Origin: null Connection: keep-alive Pragma: no-cache Cache-Control: no-cache 

问题是我发送的数据在哪里,哪里有很多馅饼?

数据应该在标题行之后的空白行之后,但我认为问题是数据不以换行符结束,因此,您无法使用.readLine()方法读取它。

在循环标题行的同时,您可以查找“Content-Length”行并获取数据的长度。 到达空白行后,请停止使用.readLine() 。 而是切换到一次读取一个字符,读取“Content-Length”标题指定的字符数。 我想你可以在这个答案中找到这方面的示例代码。

如果可以,我建议您使用库来帮助解决这个问题。 我认为Apache HTTP Core库可以帮助解决这个问题。 看到这个答案 。