听起来很奇怪,但这种方法适用于 get/大多数 POST 请求,但有时(通常最近)StringBuffer 不会捕获附加在 http 请求 (JSON) 末尾的数据。我哪里错了?
/**
* Parses a client request and calls the appropriate handler
* @throws Exception
*/
private void processClientRequest() throws Exception{
//Get the client request
BufferedReader clientRequest = new BufferedReader(new InputStreamReader(connectedClient.getInputStream()));
//Start response object
DataOutputStream responseToClient = new DataOutputStream(connectedClient.getOutputStream());
System.out.println(clientRequest);
String requestString = clientRequest.readLine();
String header = requestString;
//Break up request
StringTokenizer tokenizer = new StringTokenizer(header);
//Different request parts
String httpMethod = tokenizer.nextToken();
String httpQueryString = tokenizer.nextToken();
//Print client request
StringBuffer responseBuffer = new StringBuffer();
while (clientRequest.ready()) {
responseBuffer.append(requestString + " ");
System.out.println(requestString);
requestString = clientRequest.readLine();
}
//Process GET request
if (httpMethod.equals("GET")) {
processGETRequests(httpQueryString, requestString);
}else if (httpMethod.equals("POST")) {
processPOSTRequests(responseBuffer, httpQueryString);
}
}